jq通过对象的一个参数拿到另外一个参数

products: [
        {
            id: 190, 
            goodsId: 27, 
            proNo: "HUAWEI0A01_1", 
            spec: null, 
            storeNums: null, 
            warningLine: null, 
            marketPrice: 3199, 
            sellPrice: 3199, 
            costPrice: 3199, 
            weight: null, 
            specsKey: ";2:7;8:74;10:77;"
        }, 
        {
            id: 191, 
            goodsId: 27, 
            proNo: "HUAWEI0A01_2", 
            spec: null, 
            storeNums: null, 
            warningLine: null, 
            marketPrice: 2999, 
            sellPrice: 3199, 
            costPrice: 2999, 
            weight: null, 
            specsKey: ";2:7;8:74;10:78;"
        }, 
        {
            id: 192, 
            goodsId: 27, 
            proNo: "HUAWEI0A01_3", 
            spec: null, 
            storeNums: null, 
            warningLine: null, 
            marketPrice: 3199, 
            sellPrice: 3199, 
            costPrice: 3199, 
            weight: null, 
            specsKey: ";2:9;8:74;10:77;"
        }, 
        {
            id: 193, 
            goodsId: 27, 
            proNo: "HUAWEI0A01_4", 
            spec: null, 
            storeNums: null, 
            warningLine: null, 
            marketPrice: 2999, 
            sellPrice: 3199, 
            costPrice: 2999, 
            weight: null, 
            specsKey: ";2:9;8:74;10:78;"
        }
    ]

如上代码,如何通过specsKey拿到其他的参数?例如id或者sellPrice

阅读 3.7k
5 个回答
var specsKey = ";2:7;8:74;10:77;"
$.each(products, function(index, item) {
  if(specsKey === item.specsKey) {
    console.log(item.sellPrice);
    console.log(item.id);
  }
});
var resultArr = products.filter(function (prod) {
    return pord.specsKey === specsKey;
});
var specskey = ";2:7;8:74;10:77;";
//返回所有符合条件的对象的数组
var match = products.filter(function(item, index, array) {
    return (item.specskey.indexOf(specskey) > -1);
});
//打印所有符合条件的对象的id
match.forEach(function(item, index, array) {
    console.log('id:' + item.id);
);

var data,specsKey=XXX;
products.map(function(v){
if(v.specsKey==specsKey){
data=v;
}
})

这是一个json数据格式,取products的下id值方式:

products[i].id // i对应0,1,2....

把取得的相关参数和specsKey对比再取到对应的值就行了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题