[英]Replace the comma with semicolon in array
I have this object I have an object with an array with different values 我有这个对象,我有一个带有不同值的数组的对象
var myObj = {
"number": 10,
"general": "general",
"array": [{
"num1": 11,
"text": "text1",
}, {
"num2": 1,
"text": "text2",
}, {
"num3": 3,
"text": "text3",
} ]
};
I take back the result text1,text2,text3 using this 我使用这个取回结果text1,text2,text3
var result = myObj.array.map(function (item) {
return item.text;
});
How can I take back this result text1;text2;text3 我该如何取回结果text1; text2; text3
At the moment, you're not really getting them with commas - that's just how your console is displaying an array of values. 目前,您并没有真正使用逗号-只是控制台显示值数组的方式。 To return what you want (the items delimited by a semicolon, just use the Array.join function.
要返回您想要的内容(用分号分隔的项目,只需使用Array.join函数即可。
var result = myObj.array.map(function (item) {
return item.text;
}).join(";");
The join() method joins all elements of an array into a string.
join()方法将数组的所有元素连接到字符串中。
Syntax
句法
str = arr.join([separator = ','])
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.
如果分隔符为空字符串,则所有元素之间都将没有任何字符地连接在一起。
Join them with join
: 加入他们
join
:
var result = myObj.array.map(function (item) {
return item.text;
}).join(';');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.