如何迭代JSON并选择性地克隆部分

时间:2017-04-19 09:52:46

标签: javascript json

我有一个JSON字符串,它可能包含重复信息,但价格级别密钥除外。

[{
    "id": "368",
    "type": "inventoryitem",
    "cols": {
        "id": "04-PD",
        "name": "Product 1",
        "salesdescription": "Product 1",
        "type": {
            "name": "Inventory Item",
            "internalid": "InvtPart"
        }
        "internalid": {
            "name": "368",
            "internalid": "368"
        },
        "unitprice": 50.00,
        "pricelevel": {
            "name": "pricelevel1",
            "internalid": "1"
        }
    }
}, {
    "id": "368",
    "recordtype": "inventoryitem",
    "columns": {
        "itemid": "04-PD",
        "displayname": "Product 1",
        "salesdescription": "Product 1",
        "type": {
            "name": "Inventory Item",
            "internalid": "InvtPart"
        },
        "internalid": {
            "name": "368",
            "internalid": "368"
        },
        "unitprice": 35.29,
        "pricelevel": {
            "name": "pricelevel2",
            "internalid": "12"
        }
    }
}]

我想弄清楚的是,是否有一种简单的方法来循环遍历对象并克隆映射到pricelevel的键,其中internalid为1

并删除不是的商品?

因为在这个集合中可能有几百个,我希望有一种简单的方法来克隆和删除我不需要的东西..

任何提示非常感谢

1 个答案:

答案 0 :(得分:0)

我试图提供递归逻辑来有选择地克隆。如果这是您正在寻找的,请告诉我。

var json = 
[{
    "id": "368",
    "type": "inventoryitem",
    "cols": {
        "id": "04-PD",
        "name": "Product 1",
        "salesdescription": "Product 1",
        "type": {
            "name": "Inventory Item",
            "internalid": "InvtPart"
        },
        "internalid": {
            "name": "368",
            "internalid": "368"
        },
        "unitprice": 50.00,
        "pricelevel": {
            "name": "pricelevel1",
            "internalid": "1"
        }
    }
}, {
    "id": "368",
    "recordtype": "inventoryitem",
    "columns": {
        "itemid": "04-PD",
        "displayname": "Product 1",
        "salesdescription": "Product 1",
        "type": {
            "name": "Inventory Item",
            "internalid": "InvtPart"
        },
        "internalid": {
            "name": "368",
            "internalid": "368"
        },
        "unitprice": 35.29,
        "pricelevel": {
            "name": "pricelevel2",
            "internalid": "12"
        }
    }
}];

function getMatchParentChildKeyVal(json, parentKey, matchKey, matchVal) {

  var out = [];
  
  for (var i = 0 ; i < json.length; i++) {
  
    var matchRecord = parseMatchParent(json[i],null,parentKey); 
    if (matchRecord) {
        if (matchRecord[matchKey] == matchVal) {
            out.push(json[i]);
        }
    }
  }
  return out;
}
  
 function parseMatchParent(tree, parent, match) {
 
  for (var k in tree){
    if (tree.hasOwnProperty(k)) {
       if (k == match) return tree[k];
        else if (typeof tree[k] == 'object') {
           var childMatch = parseMatchParent(tree[k],k, match);
           if (childMatch) return childMatch;
           }
    }
  }
  return false;
 }
 
 console.log(getMatchParentChildKeyVal(json,"pricelevel","internalid",'1'));