如何在不知道密钥

时间:2018-02-14 21:48:08

标签: javascript json reactjs

我有这个json对象

{
  "index": {
    "queryString": "",
    "filtersMap": {},
    "selectedFilters": [],
    "queries": [
      {
        "filtered": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "listingRecordState.id": "3"
                  }
                },
                {
                  "geo_distance": {
                    "distance": "100mi",
                    "property.location": {
                      "lat": "40.7608",
                      "lon": "-111.8910"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ],
    "filters": [],
    "_source": null,
    "size": 9
  },
  "query": {
    "query": {
      "filtered": {
        "filter": {
          "bool": {
            "must": [
              {
                "term": {
                  "listingRecordState.id": "3"
                }
              },
              {
                "geo_distance": {
                  "distance": "100mi",
                  "property.location": {
                    "lat": "40.7608",
                    "lon": "-111.8910"
                  }
                }
              }
            ]
          }
        }
      }
    },
    "size": 9
  }
}

我需要能够在不知道确切路径的情况下更新此对象内的lat和long,因为每个数组中的索引都可能发生变化。

只有一个键" lat"和#34; long"所以重复的密钥名称不会有问题。

我尝试了几种使用循环和递归的解决方案,但却无法创建或找到有效的东西。

谢谢!

1 个答案:

答案 0 :(得分:0)

这是一种方法,但在这种情况下,指定键的所有实例都将被更新 - 所以要小心,因为之前的评论是正确的,你有' lon'和' lat'您发布的数据中有两次。



var data =  {
      "index": {
        "queryString": "",
        "filtersMap": {},
        "selectedFilters": [],
        "queries": [
          {
            "filtered": {
              "filter": {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "listingRecordState.id": "3"
                      }
                    },
                    {
                      "geo_distance": {
                        "distance": "100mi",
                        "property.location": {
                          "lat": "40.7608",
                          "lon": "-444.444"
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        ],
        "filters": [],
        "_source": null,
        "size": 9
      },
      "query": {
        "query": {
          "filtered": {
            "filter": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "listingRecordState.id": "3"
                    }
                  },
                  {
                    "geo_distance": {
                      "distance": "100mi",
                      "property.location": {
                        "lat": "40.7608",
                        "lon": "-111.8910"
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        "size": 9
      }
    }
    
function updateByKey(obj, key, replace) {

  var j, key = key || '',
    obj = obj || {},
    keys = key.split("."),
    sObj = [],
    ssObj = [],
    isSelector = !!(keys.length > 0);

  var findKey = function(obj, key) {
    var k;
    for (k in obj) {
      if (k === key) {
        sObj.push(obj[k]);
        
        if (replace) obj[k] = replace
        
      } else if (typeof obj[k] == 'object') {
        findKey(obj[k], key);
      }
    }
  };

  if (isSelector) {
    var nKey = keys.shift();
    findKey(obj, nKey);

    while (keys.length > 0) {
      nKey = keys.shift();

      if (sObj.length > 0) {
        ssObj = sObj.slice(0), sObj = [];
        for (j in ssObj) {
          findKey(ssObj[j], nKey);
        }
      }
    }
  } else {
    findKey(obj, key);
  }

  // return occurrences of key in array
  return (sObj.length === 1) ? sObj.pop() : sObj;
};

var keyToFind = 'lon';
var replace = 5555;

console.log(updateByKey(data,'lon',replace))
console.log(updateByKey(data,'lon'))

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
&#13;
&#13;
&#13;