[英]Split an array into an array with different objects
I was working on a plain array but I want the structure of the array to be in a sub-array form with multiple entries inside it.我正在研究一个普通数组,但我希望数组的结构采用子数组形式,其中包含多个条目。 I tried forEach but when I push the element the whole array is pushed inside the sub-array.
我尝试了 forEach 但是当我推送元素时,整个数组被推入子数组中。 The array that I have
我拥有的数组
data = ["1640662522","1640663240","1640663967","1640664659","1640665388","1640666124","1640666829","1640667505","1640668278","1640668984"];
but I want the structure of my new array to be但我希望我的新数组的结构是
{
"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
}
]
Could anyone help me on this.谁能帮我解决这个问题。 I am working on js for this
我正在为此研究js
Just do it.去做就对了。
Step 1: Create new array elements
第 1 步:创建新的数组元素
Step 2: ForEach data and push data to array elements
步骤 2:ForEach 数据并将数据推送到数组元素
Step 3: Standardized data.
第 3 步:标准化数据。
Good luck!祝你好运!
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.