[英]JavaScript: How to get all object values of an object without iterator
I convert multidimensional array to object and when I try to return the converted object, I get only the first row values.我将多维数组转换为 object,当我尝试返回转换后的 object 时,我只得到第一行值。
Here is my code.这是我的代码。
const price = [
[
[1642636800000, 6.479682086929113],
[1642723200000, 6.34357451008912],
[1642809600000, 5.121543485810777],
[1642896000000, 3.9740756848518095],
[1642982400000, 4.3575187509716],
[1643068800000, 3.9012325159931027],
[1643155200000, 3.4317289914580345],
[1643241600000, 3.2757454409610474],
[1643328000000, 2.638504600238906],
[1643414400000, 2.7780564535948247],
[1643500800000, 2.6434238272520734],
[1643587200000, 2.4336766397631195],
[1643673600000, 2.3564650737278945],
[1643760000000, 2.0999935033955395],
[1643846400000, 2.030984947088804],
[1643932800000, 1.9601855774194021],
[1644019200000, 1.939260102893443],
[1644105600000, 1.9099357711674574],
[1644192000000, 1.756137354391081],
[1644278400000, 1.5997203967365643],
[1644364800000, 1.5123161188548575],
[1644451200000, 1.519374648506311],
[1644537600000, 1.537046985055748],
[1644624000000, 1.508372954955599],
[1644710400000, 1.55420714318404],
[1644796800000, 1.5875532111414001],
[1644883200000, 1.6653755421073082],
[1644969600000, 1.6647861014789664],
[1645056000000, 1.7214785428910968],
[1645142400000, 1.7801924052482303],
[1645177320000, 1.7624504335296196],
],
];
code to convert price
array to JSON object将price
数组转换为 JSON object 的代码
const prices = price.map(function (value, key) {
return {
first: value[0][1],
second: value[0][1],
};
});
console.log(prices); //prints only the first rows
How can I get all rows of the object?如何获取 object 的所有行?
Thanks谢谢
the problem here is that you have three dimensional array so when you map over it, it will only map over the elements inside first array to do what you want you can try this这里的问题是你有三维数组所以当你 map 超过它时,它只会 map 超过第一个数组中的元素来做你想做的事你可以试试这个
price.map(function (item) {
return item.map(function (item) {
return { first: item[0], second: item[1] };
});
})
:) :)
const prices = price[0].map(function(value, key) {
return {
first: value[0],
second: value[1],
};
});
const pricesArr = [ [ [1642636800000, 6.479682086929113], [1642723200000, 6.34357451008912], [1642809600000, 5.121543485810777], [1642896000000, 3.9740756848518095], [1642982400000, 4.3575187509716], [1643068800000, 3.9012325159931027], [1643155200000, 3.4317289914580345], [1643241600000, 3.2757454409610474], [1643328000000, 2.638504600238906], [1643414400000, 2.7780564535948247], [1643500800000, 2.6434238272520734], [1643587200000, 2.4336766397631195], [1643673600000, 2.3564650737278945], [1643760000000, 2.0999935033955395], [1643846400000, 2.030984947088804], [1643932800000, 1.9601855774194021], [1644019200000, 1.939260102893443], [1644105600000, 1.9099357711674574], [1644192000000, 1.756137354391081], [1644278400000, 1.5997203967365643], [1644364800000, 1.5123161188548575], [1644451200000, 1.519374648506311], [1644537600000, 1.537046985055748], [1644624000000, 1.508372954955599], [1644710400000, 1.55420714318404], [1644796800000, 1.5875532111414001], [1644883200000, 1.6653755421073082], [1644969600000, 1.6647861014789664], [1645056000000, 1.7214785428910968], [1645142400000, 1.7801924052482303], [1645177320000, 1.7624504335296196], ], ]; const prices = pricesArr.flat(1).map(price => ({ first: price[0], second: price[1] })); console.log(prices);
You have a nested array, you can flatten it first and then, iterate to create an array of object.你有一个嵌套数组,你可以先将它展平,然后迭代创建一个 object 的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.