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
240 views
in Technique[技术] by (71.8m points)

javascript - 从JavaScript中的对象获取值[重复](Get values from an object in JavaScript [duplicate])

This question already has an answer here:(这个问题在这里已有答案:)

How to get all properties values of a Javascript Object (without knowing the keys)?(如何获取Javascript对象的所有属性值(不知道密钥)?) 20 answers(20个答案)

I have this object:(我有这个对象:)

var data = {"id": 1, "second": "abcd"}; These are values from a form.(这些是表格中的值。) I am passing this to a function for verification.(我将此传递给函数进行验证。) If the above properties exist we can get their values with data["id"] and data["second"] , but sometimes, based on other values, the properties can be different.(如果存在上述属性,我们可以使用data["id"]data["second"]获取它们的值,但有时,基于其他值,属性可能不同。) How can I get values from data independent of property names?(如何从data获取与属性名称无关的值?)   ask by Hari krishnan translate from so

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

1 Answer

0 votes
by (71.8m points)

To access the properties of an object without knowing the names of those properties you can use a for ... in loop:(要在不知道这些属性的名称的情况下访问对象的属性,可以使用for ... in循环:)

for(key in data) { if(data.hasOwnProperty(key)) { var value = data[key]; //do something with value; } }

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

...