为了账号安全,请及时绑定邮箱和手机立即绑定

js遍历json根据id选择属性值显示对应的div?

js遍历json根据id选择属性值显示对应的div?

猛跑小猪 2019-04-27 19:15:35
有3个json,它们之间可以找到相同的id,我想要的是选择了属性1下面的属性值,然后再选择属性2下面的属性值(属性1是必有的,但是属性2可能有的情况下没有),请问我该怎么写js可以实现?//属性1var 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",}//属性2var 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"}}
查看完整描述

3 回答

?
慕码人2483693

TA贡献1860条经验 获得超9个赞

建议使用markdown语法格式化一下JS代码,提高适读性,如:

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
        "
    }
}
<dl>
    <dt>
        <label>属性1</label>
    </dt>
    <dd>
        <select name="super_attribute[1]" id="attribute1">
            <option value="">Choose an Option...</option>
            <option value="2">0.04Ohm</option>
            <option value="3">0.05Ohm</option>
        </select>
    </dd>
    <dt>
        <label>属性2</label>
    </dt>
    <dd>
        <select name="super_attribute[7]" id="attribute7">
            <option value="">Choose an Option...</option>
            <option value="8">Black</option>
            <option value="9">Red</option>
        </select>
    </dd>
    <dt>
        <label>Warehouse</label>
    </dt>
    <dd>
        <!--根据上面选择的属性值显示对应的仓库内容-->
    </dd>
</dl>
<dl>
    <dt>
        <label>属性1</label>
    </dt>
    <dd>
        <select name="super_attribute[1]" id="attribute1">
            <option value="">Choose an Option...</option>
            <option value="2">0.04Ohm</option>
            <option value="3">0.05Ohm</option>
        </select>
    </dd>
    <dt>
        <label>属性2</label>
    </dt>
    <dd>
        <select name="super_attribute[7]" id="attribute7">
            <option value="">Choose an Option...</option>
            <option value="8">Black</option>
            <option value="9">Red</option>
        </select>
    </dd>
    <dt>
        <label>Warehouse</label>
    </dt>
    <dd>
        <!--根据上面选择的属性值显示对应的仓库内容-->
    </dd>
</dl>


查看完整回答
反对 回复 2019-05-08
?
森栏

TA贡献1810条经验 获得超5个赞

知识考察点:遍历对象,遍历数组的用法

案例:
var A = {a:1,b:2,c:3,d:"hello world"};

for(var k in A) {

console.log(k,A[k]);


输出内容:
a 1
b 2
c 3
d hello world


查看完整回答
反对 回复 2019-05-08
  • 3 回答
  • 0 关注
  • 529 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信