0

i have this:

    var keys = ['foo', 'bar', 'baz'];
    var values = [11, 22, 33]

    var result = {};
    keys.forEach((key, i) => result[key] = values[i]);
    console.log(result);

which is the right way to push the keys ans the values back to separated arrays from json result?

    result = {"foo:11,"bar":22,"baz":33}
    keys = ['foo', 'bar', 'baz'];
    values = [11, 22, 33]
diego jimenez
  • 73
  • 1
  • 5

1 Answers1

1

basically, for Objects in javascript you can use 2 useful functions called Object.keys and Object.values, you can check the documentation to see if any other function can work for you.

check the Methods section

const result = {
  "foo": 11,
  "bar": 22,
  "baz": 33
}

const keys = Object.keys(result);
const values = Object.values(result);

console.log("here you have the keys")
console.log(keys)
console.log("here you have the values")
console.log(values)
Prince Hernandez
  • 3,623
  • 1
  • 10
  • 19