我有这个对象 我有一个对象,其中包含具有不同值的数组
var myObj = {
"number": 10,
"general": "general",
"array": [{
"num1": 11,
"text": "text1",
}, {
"num2": 1,
"text": "text2",
}, {
"num3": 3,
"text": "text3",
} ]
};
我用这个取回结果text1,text2,text3
var result = myObj.array.map(function (item) {
return item.text;
});
如何取回这个结果text1;text2;text3
最佳答案
目前,您并没有真正用逗号来获取它们 - 这只是您的控制台显示值数组的方式。要返回您想要的内容(以分号分隔的项目,只需使用 Array.join 函数即可。
var result = myObj.array.map(function (item) {
return item.text;
}).join(";");
来自MDN ;
The join() method joins all elements of an array into a string.
Syntax
str = arr.join([separator = ','])
Parameters
separator - Optional. Specifies a string to separate each element of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a comma. If separator is an empty string, all elements are joined without any characters in between them.
关于javascript - 将数组中的逗号替换为分号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31742993/