Home > other >  JavaScript: How to get all object values of an object without iterator
JavaScript: How to get all object values of an object without iterator

Time:02-19

I convert multidimensional array to object and when I try to return the converted object, I get only the first row values.

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

 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?

Thanks

CodePudding user response:

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

    price.map(function (item) {
    return item.map(function (item) {
      return { first: item[0], second: item[1] };
    });
  })

CodePudding user response:

:)

const prices = price[0].map(function(value, key) {
  return {
    first: value[0],
    second: value[1],
  };
});

CodePudding user response:

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.

  • Related