将数组拆分为具有不同对象的数组
Split an array into an array with different objects
我正在处理一个普通数组,但我希望该数组的结构采用其中包含多个条目的子数组形式。我尝试了 forEach,但是当我推送元素时,整个数组被推送到子数组中。
我的数组
data = ["1640662522","1640663240","1640663967","1640664659","1640665388","1640666124","1640666829","1640667505","1640668278","1640668984"];
但我希望新数组的结构是
{
"sizeData":[
{
"timestamp":1640662522,
"size":12345,
"fees":12589,
"cost":168
},
{
"timestamp":1640663240,
"size":12345,
"fees":12589,
"cost":168},
{
"timestamp":1640663967,
"size":12345,
"fees":12589,
"cost":168
}, {
"timestamp":1640664659,
"size":12345,
"fees":12589,
"cost":168
}
]
谁能帮我解决这个问题。
我正在为此开发 js
就去做吧。
Step 1: Create new array elements
Step 2: ForEach data and push data to array elements
Step 3: Standardized data.
祝你好运!
let data = ["1640662522","1640663240","1640663967","1640664659","1640665388","1640666124","1640666829","1640667505","1640668278","1640668984"];
let elements = [];
data.forEach(function(val){
elements.push({
"timestamp":parseInt(val),
"size":12345,
"fees":12589,
"cost":168
})
})
let result = {
"sizeData": elements
}
console.log(result)
我正在处理一个普通数组,但我希望该数组的结构采用其中包含多个条目的子数组形式。我尝试了 forEach,但是当我推送元素时,整个数组被推送到子数组中。 我的数组
data = ["1640662522","1640663240","1640663967","1640664659","1640665388","1640666124","1640666829","1640667505","1640668278","1640668984"];
但我希望新数组的结构是
{
"sizeData":[
{
"timestamp":1640662522,
"size":12345,
"fees":12589,
"cost":168
},
{
"timestamp":1640663240,
"size":12345,
"fees":12589,
"cost":168},
{
"timestamp":1640663967,
"size":12345,
"fees":12589,
"cost":168
}, {
"timestamp":1640664659,
"size":12345,
"fees":12589,
"cost":168
}
]
谁能帮我解决这个问题。 我正在为此开发 js
就去做吧。
Step 1: Create new array elements
Step 2: ForEach data and push data to array elements
Step 3: Standardized data.
祝你好运!
let data = ["1640662522","1640663240","1640663967","1640664659","1640665388","1640666124","1640666829","1640667505","1640668278","1640668984"];
let elements = [];
data.forEach(function(val){
elements.push({
"timestamp":parseInt(val),
"size":12345,
"fees":12589,
"cost":168
})
})
let result = {
"sizeData": elements
}
console.log(result)