我有一个包含不同值数组的对象
var myObj = {
"number": 10,
"general": "general",
"array": [{
"num1": 11,
"text": "text1",
}, {
"num2": 1,
"text": "text2",
}, {
"num3": 3,
"text": "text3",
} ]
};
怎么可能得到像text1,text2,text3这样的结果
我试试这个
for (i=0; i <myObj.array.length; i++) {
a = myObj.array[i].text
a1 += a
}
最佳答案
使用Array.prototype.map
方法
var result = myObj.array.map(function (item) {
return item.text;
});
如果你需要结果作为字符串,你可以使用join
方法:
result.join(',');
关于javascript - 在对象中取数组的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31728925/