我有一个从console.log(temp)显示的数组,如下所示:
temp = [{name: "Request A", data: "1"}
{name: "Request B", data: "12"}
{name: "Request C", data: "6"}]
如果我想更改数组格式应该怎么做?
temp: [{name: "Request A", data: [1]}
{name: "Request B", data: [12]}
{name: "Request C", data: [6]}]
请帮助大家...谢谢
答案 0 :(得分:3)
使用'IsUserText
函数将返回一个新数组,并使用map
将字符串转换为数字
parseInt
答案 1 :(得分:2)
要更改数组,您可以遍历<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="fruit">
<option>Apple</option>
<option>Pear</option>
</select>
的项目,只需更改属性即可:
forEach
答案 2 :(得分:0)
您可以使用map
let temp = [{name: "Request A", data: "1"},{name: "Request B", data: "12"},{name: "Request C", data: "6"}]
let op = temp.map(e=> {
return {
...e,
data : [+e.data]
}
})
console.log(op)
答案 3 :(得分:0)
尝试一下
temp = [{name: "Request A", data: "1"},
{name: "Request B", data: "12"},
{name: "Request C", data: "6"}];
let temp1 = temp.map(item => {
item.data = [parseInt(item.data)]
return item;
})
console.log(temp1);
输出:
[ { "name": "Request A", "data": [ 1 ] }, { "name": "Request B", "data": [ 12 ] }, { "name": "Request C", "data": [ 6 ] } ]
答案 4 :(得分:0)
尝试
temp.forEach(x=> x.data=[+x.data]);
let temp = [{name: "Request A", data: "1"},
{name: "Request B", data: "12"},
{name: "Request C", data: "6"}];
temp.forEach(x=> x.data=[+x.data]);
console.log(JSON.stringify(temp));
答案 5 :(得分:0)
尝试一下- 我正在使用Array.of创建一个数组。 +运算符可将字符串解析为Array。
temp.map(t => (t.data = Array.of(+t.data), t));
答案 6 :(得分:0)
这是一种方法。这是来自节点REPL。
> var temp = [{name: "Request A", data: "1"}, {name: "Request B", data: "12"}, {name: "Request C", data: "6"}];
undefined
> temp.forEach(function(e) {
..... e.data = [parseInt(e.data, 10)];
..... });
undefined
> temp
[ { name: 'Request A', data: [ 1 ] },
{ name: 'Request B', data: [ 12 ] },
{ name: 'Request C', data: [ 6 ] } ]
>