Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

javascript - 对象中的JavaScript对象(JavaScript object in object)

I have a set of object of object.(我有一组对象。)

For example(例如) { "123":{ id:123, name:"abc" }, "456":{ id:456, name:"def" }, "789":{ id:789, name:"ghi" } } I would like to know how to loop over my object and check if the value "def" exist in the object list?(我想知道如何遍历我的对象并检查对象列表中是否存在值“ def”?) Can I know how to loop through every iteration and only do decision ??(我能知道如何在每次迭代中循环并仅做决定吗?) For example first iteration is abc then next is def then next is ghi .(例如,第一个迭代是a??bc,然后是def,然后是ghi。) because abc and def is not same but when come to def and def it is same .Can I do action or logic after finish loop through every iteration ?(因为abc和def不相同,但是当def和def相同时。我可以在每次迭代完成循环后执行动作或逻辑吗?)   ask by You Unknown translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use a for loop on the object to check that value exist in name property or not:(在对象上使用for循环检查name属性中是否存在该值:)

var obj = { 123: { id: 123, name: 'abc' }, 456: { id: 456, name: 'def' }, 789: { id: 789, name: 'ghi' } }; var checkVal = 'def'; let match = false; for(var objKey in obj) { if(obj[objKey].name === checkVal) { match = true; } } console.log('found ', match);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...