有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#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真
- ¥15 arduino 四自由度机械臂
- ¥15 wordpress 产品图片 GIF 没法显示
- ¥15 求三国群英传pl国战时间的修改方法
- ¥15 matlab代码代写,需写出详细代码,代价私
- ¥15 ROS系统搭建请教(跨境电商用途)
- ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。