简体   繁体   English

JavaScript:如何在没有迭代器的情况下获取 object 的所有 object 值

[英]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 objectprice数组转换为 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.

相关问题 如何获取 JavaScript Object 的所有属性值(不知道密钥)? - How to get all properties values of a JavaScript Object (without knowing the keys)? JavaScript:如何在不替换对象的情况下有效地替换对象的所有值 - JavaScript: How to efficiently replace all values of an object without replacing the object Javascript获取对象的所有值 - Javascript get all values of object 展开运算符如何使用 Javascript 中的迭代器/生成器从对象中获取值? - How does spread operator get values from an object with an Iterator/Generator in Javascript? Symbol.iterator:获取迭代器对象中所有对象的属性 - Symbol.iterator: get all the object's properties in iterator object 如何从 JavaScript(HTML) 中的 JSON 对象获取没有键的值 - How to get values without key from JSON object in JavaScript(HTML) 是否可以在ES5 JavaScript中实现对象键的惰性迭代器而无需将对象的所有键立即加载到内存中? - Can a lazy iterator for the keys of an object be implemented in ES5 JavaScript without loading all keys of the object into memory at once? 如何获取嵌套JavaScript object中某个键的所有值? - How to get all the values of a key in a nested JavaScript object? 如何通过键获取Javascript对象的所有值? - How to get all values of a Javascript Object by its keys? 从Javascript对象获取所有Last值 - Get all Last values from Javascript Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM