基于内部数组属性过滤JSON对象



我有以下JSON对象我需要根据内部数组的标头和键过滤响应,以便"ISVN8JF1E" == “key”: ISVN8JF1E

{
  "1233-39CFBWYA": [],
  "JMSK0DKOE": [],
  "ISVN8JF1E": [
    {
      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": "LA",
      "hello": "OUTSIDE",
      "key": "ISVN8JF1E"
    },
    {
      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": "Chicago",
      "hello": "Inside",
      "key": "ISVN8JF1E"
    },
    {
      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": "Austin",
      "hello": "Inside",
      "key": "ABCDE"
    }
  ],
 "ISVN8JF1B": [
    {
      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": "LA",
      "hello": "OUTSIDE",
      "key": "ISVN8JF1B"
    },
    {
      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": "Chicago",
      "hello": "Inside",
      "key": "ISVN8JF1C"
    },
    {
      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": "Austin",
      "hello": "Inside",
      "key": "ABCDE"
    }
  ],
  "OGAESJF2EEAD3W398ZNOSA": [],
  "SC9OMJF2EEAD3W398ZNOSA": []
  }

这是我尝试的:

var result = object.keys(data).foreach(function(id){

                var updatedResult = {
                    [Id] : data[Id].filter(function (my) {
                        return my.key == Id;
                    })
                };
                totalResultArray.push(updatedResult);
            });

有没有办法做到这一点?

您可以检查给定的键并过滤键是否设置。

var data = { "1233-39CFBWYA": [], "JMSK0DKOE": [], "ISVN8JF1E": [{ delloData: "1478644629", ref: "75", dataType: "String", somePart: "LA", hello: "OUTSIDE", key: "ISVN8JF1E" }, { delloData: "1478644629", ref: "75", dataType: "String", somePart: "Chicago", hello: "Inside", key: "ISVN8JF1E" }, { delloData: "1478644629", ref: "75", dataType: "String", somePart: "Austin", hello: "Inside", key: "ABCDE" }], "OGAESJF2EEAD3W398ZNOSA": [], "SC9OMJF2EEAD3W398ZNOSA": [] },
    key = 'ISVN8JF1E',
    result = (data[key] || []).filter(function (o) {
        return o.key === key;
    });
console.log(result);

var blob = {
  "1233-39CFBWYA": [],
  "JMSK0DKOE": [],
  "ISVN8JF1E": [{
    "delloData": "1478644629",
    "ref": "75",
    "dataType": "String",
    "somePart": "LA",
    "hello": "OUTSIDE",
    "key": "ISVN8JF1E"
  }, {
    "delloData ": "1478644629 ",
    "ref ": "75 ",
    "dataType ": "String ",
    "somePart ": "Chicago ",
    "hello ": "Inside",
    "key": "ISVN8JF1E"
  }, {
    "delloData ": "1478644629 ",
    "ref ": "75 ",
    "dataType ": "String ",
    "somePart ": "Austin ",
    "hello ": "Inside",
    "key": "ABCDE"
  }],
  "OGAESJF2EEAD3W398ZNOSA ": [],
  "SC9OMJF2EEAD3W398ZNOSA ": []
};
var match_key = "ISVN8JF1E";
var filtered = blob["ISVN8JF1E"].filter(function(val) {
  return val.key == match_key;
});
console.log(filtered);

最新更新