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

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


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

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

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


var resultArr = products.filter(function (prod) {
    return pord.specsKey === specsKey;
});

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


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 specsKey = ";2:7;8:74;10:77;"
$.each(products, function(index, item) {
  if(specsKey === item.specsKey) {
    console.log(item.sellPrice);
    console.log(item.id);
  }
});
【热门文章】
【热门文章】