javascript - jq通过对象的一个参数拿到另外一个参数
阿神
阿神 2017-04-10 17:51:42
[JavaScript讨论组]
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

阿神
阿神

闭关修行中......

全部回复(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);
);
PHPz

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对比再取到对应的值就行了。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号