[Solved] javascript: Object and array manipulation i


The original example input has a few syntactical issues so I am not sure as to what the “array” and its elements look like. Making an assumption to make it syntactically compliant with the fewest modifications leads to the array in the working example below. Based on that input, using Object.values is the simplest way to get the desired output.

const arr = [{
  "0": {
    "name": "a",
    "available": "12",
    "onOrder": "0",
  },
  "1": {
    "name": "b",
    "available": "3",
    "onOrder": "0"
  },
  "2": {
    "name": "c",
    "available": "0",
    "onOrder": "3"
  },
  "3": {
    "name": "d",
    "available": "2",
    "onOrder": "2"
  }
}];

console.log(Object.values(arr[0]));

EDIT: Updated to show ES2015 compatible solution

const arr = [{
  "0": {
    "name": "a",
    "available": "12",
    "onOrder": "0",
  },
  "1": {
    "name": "b",
    "available": "3",
    "onOrder": "0"
  },
  "2": {
    "name": "c",
    "available": "0",
    "onOrder": "3"
  },
  "3": {
    "name": "d",
    "available": "2",
    "onOrder": "2"
  }
}];

console.log(Object.keys(arr[0]).map(k => arr[0][k]));

2

solved javascript: Object and array manipulation i