代码之家  ›  专栏  ›  技术社区  ›  pploypiti

从特定的json字段提取数据,将其用作变量,并更新字段值?

  •  -1
  • pploypiti  · 技术社区  · 7 年前

    我需要将origin值更改为lat/lang坐标(来自另一个geojson文件),而不是country name:

    index = [
      {
        "Name": "Aish Merahrah",
        "Origin": "Egypt",
        "Description": "Made with fenugreek seeds and maize; dough allowed to 
    ferment overnight, then flattened and baked."
      },
      {
        "Name": "Ajdov Kruh",
        "Origin": "Slovenia",
        "Description": "Made with buckwheat flour and potato."
      }
    ]
    

    结果会是:

      {
        "Name": "Ajdov Kruh",
        "Origin": "46.151241, 14.995463",
        "Description": "Made with buckwheat flour and potato."
      }
    

    我不确定工作流程,是否需要提取json值变量并以某种方式使用该变量来获取lat&lang数据?顺便说一句 需要 只使用js/jquery和node。

    3 回复  |  直到 7 年前
        1
  •  0
  •   kiddorails    7 年前

    假设您的geojson具有以下结构(country name作为key,lat/long作为value):

    geolocation = {"Slovenia": "46.151241, 14.995463", "Egypt": "1, 1"}
    

    你可以使用 Array.prototype.map 迭代 index 以及替换 Origin 每个对象的值 geolcoation .

    index = [
      {
        "Name": "Aish Merahrah",
        "Origin": "Egypt",
        "Description": "Made with fenugreek seeds and maize; dough allowed to ferment overnight, then flattened and baked."
      },
      {
        "Name": "Ajdov Kruh",
        "Origin": "Slovenia",
        "Description": "Made with buckwheat flour and potato."
      }
    ]
    geolocation = {"Slovenia": "46.151241, 14.995463", "Egypt": "1, 1"};
    result = index.map(function(obj) { obj['Origin'] = geolocation[obj['Origin']]; return obj }); 
    console.log(result);

    你甚至可以使用 forEach 若要在上面迭代,它仍将更改 指数 ,因为更改是在引用对象中完成的。

        2
  •  1
  •   Nikhil Aggarwal    7 年前

    使用 Array.forEach

    let index = [{"Name":"Aish Merahrah","Origin":"Egypt","Description":"Made with fenugreek seeds and maize; dough allowed to ferment overnight, then flattened and baked."},{"Name":"Ajdov Kruh","Origin":"Slovenia","Description":"Made with buckwheat flour and potato."}];
    let lat = {"Slovenia": "46.151241, 14.995463", "Egypt": "26.820553, 30.802498"};
    index.forEach(o => o.Origin = lat[o.Origin] ?  lat[o.Origin] :  o.Origin); 
    console.log(index);
        3
  •  0
  •   Alex    7 年前

    只需遍历对象索引,然后用值编辑值。

    假设给定的json数据:


    // Creates an Object
    let index = [
      {
        "Name": "Aish Merahrah",
        "Origin": "Egypt",
        "Description": "Made with fenugreek seeds and maize; dough allowed to ferment overnight, then flattened and baked."
      },
      {
        "Name": "Ajdov Kruh",
        "Origin": "Slovenia",
        "Description": "Made with buckwheat flour and potato."
      }
    ]
    
    console.log(`Original Origin 1: ${index[0].Origin}\nOriginal Origin 2: ${index[1].Origin}`);
    
    // Iterate over indices
    for(let i=0;i<index.length;i++) {
      // Edit the value
      index[i].Origin = "1234/1234"
    }
    
    /*
    // Using forEach
    index.forEach((elem) => {
      elem.Origin = "Your Value";
    });
    */
    
    console.log(`Edited Origin 1: ${index[0].Origin}\nEdited Origin 2: ${index[1].Origin}`);