有3个json,它们之间可以找到相同的id,我想要的是选择了属性1下面的属性值,然后再选择属性2下面的属性值(属性1是必有的,但是属性2可能有的情况下没有),请问我该怎么写js可以实现?
//属性1
var arr1 = {
"attributes": {
"1": {
"id": "1",
"options": [{
"id": "2",
"products": ["4", "5"]
}, {
"id": "3",
"label": "0.05Ohm",
"price": "3",
"oldPrice": "3",
"products": ["6"]
}]
},
"7": {
"id": "7",
"options": [{
"id": "8",
"products": ["4"]
}, {
"id": "9",
"products": ["10"]
}]
}
},
"template": "price",
}
//属性2
var arr2 = {
"7":{
"8":[
"4"
],
"9":[
"10"
]
},
"1":{
"2":[
"4",
"5"
],
"3":[
"6"
]
}
}
//仓库
var arr3={
"4":{
"qty":"5",
"html":"Warehouse1”
},
"5":{
"qty":"6",
"html":"Warehouse2"
},
"6":{
"qty":"22",
"html":"Warehouse3"
},
"10":{
"qty":"13",
"html":"Warehouse4"
}
}
js遍历json根据id选择属性值显示对应的div?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- DRACULAX05 2019-03-28 19:15关注
我觉得你的需求应该是根据arr2的输入去得到对应的arr3里的库存信息吧,其中arr1是作为商品表作为过渡
废话不说,代码如下Array.intersect = function(arr1, arr2) { if(Object.prototype.toString.call(arr1) === "[object Array]" && Object.prototype.toString.call(arr2) === "[object Array]") { return arr1.filter(function(v){ return arr2.indexOf(v)!==-1 }) } } var i = 0; for(var k1 in arr2) { for(var k2 in arr2[k1]) { var options = arr1.attributes[k1].options; var found = false; for(i = 0; i < options.length; i++) { if(options[i].id == k2) { found = true; break; } } if(!found) continue; var products = options[i].products; // 求商品表和购买表的交集 products = Array.intersect(products, arr2[k1][k2]); for(i = 0; i < products.length; i++){ console.log(k1 + ":" + k2 + ":" + products[i] + "="); console.log(arr3[products[i]]); } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 #MATLAB仿真#车辆换道路径规划
- ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
- ¥15 数据可视化Python
- ¥15 要给毕业设计添加扫码登录的功能!!有偿
- ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
- ¥15 微信公众号自制会员卡没有收款渠道啊
- ¥100 Jenkins自动化部署—悬赏100元
- ¥15 关于#python#的问题:求帮写python代码
- ¥20 MATLAB画图图形出现上下震荡的线条
- ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘