简体   繁体   中英

How to convert object into array in Javascript

I have the below object obj(coming as a JSON response):

var obj = {
  0: {
    note: 'test1',
    id: 24759045,
    createTimeStamp: '2022-08-01T17:05:36.750Z',
  },
  1: {
    note: 'test2',
    id: 24759045,
    createTimeStamp: '2022-08-01T17:05:51.755Z',
  },
  note: 'test1',
  id: 24759045,
  createTimeStamp: '2022-08-01T17:05:36.750Z',
};

I only want the objects with numbers("0", "1".. so on) to be pushed in an array.

Below is what I am trying to do:

let items = [];
for (var prop in obj) {
  items.push(obj[prop]);
}
console.log(items);

// expected output:

[
  {
    note: 'test1',
    id: 24759045,
    createTimeStamp: '2022-08-01T17:05:36.750Z',
  },
  {
    note: 'test2',
    id: 24759045,
    createTimeStamp: '2022-08-01T17:05:51.755Z',
  },
]

Any pointers would be highly appreciated.

A few things to consider here.

  • Are the numeric keys ordered?
  • Does the order matter?
  • Are the numeric keys an index of the item in the array?
  • Are there any gaps in the numeric keys?

First solution, assuming that the numeric keys are the index in the array.

const items = Object.keys(obj).reduce((acc, key) => {
  const index = parseInt(key);
  if (Number.isNaN(index)) {
    return acc;
  }
  acc[index] = obj[key];
  return acc;
}, []);

Second solution, assuming that order matters, but that the numeric keys are not guaranteed to be contiguous.

const items = Object.keys(obj)
  .filter((key) => Number.isNaN(parseInt(key)) === false)
  .sort()
  .map((key) => obj[key]);

Keep in mind that Object.keys does not guarantee that the keys are ordered alpha-numerically. So if order matters, then you have to sort them.

Third solution, if order doesn't matter.

const items = Object.keys(obj)
  .filter((key) => Number.isNaN(parseInt(key)) === false)
  .map((key) => obj[key]);

I think you'll need to get the keys of the object, filter out the non-numeric ones, then map each key to the obj[key]:

 var obj={"0":{"note":"test1","id":24759045, "createTimeStamp":"2022-08-01T17:05:36.750Z"},"1":{"note":"test2","id":24759045, "createTimeStamp":"2022-08-01T17:05:51.755Z"}, "note":"test1","id":24759045,"createTimeStamp":"2022-08-01T17:05:36.750Z"}; console.log( Object.keys(obj).filter((key) =>.Number.isNaN(parseInt(key))) .map((key) => obj[key]) )

 var result = []; var obj = { "0": { "note": "test1", "id": 24759045, "createTimeStamp": "2022-08-01T17:05:36.750Z" }, "1": { "note": "test2", "id": 24759045, "createTimeStamp": "2022-08-01T17:05:51.755Z" }, "note": "test1", "id": 24759045, "createTimeStamp": "2022-08-01T17:05:36.750Z" } for (var i in obj) result.push(obj[i]); $('#result').html(JSON.stringify(result));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="result"></div>

You can achieve this by doing the following steps.

Copied your object below -->

var obj = {
  "0": {
    "note": "test1",
    "id": 24759045,
    "createTimeStamp": "2022-08-01T17:05:36.750Z"
  },
  "1": {
    "note": "test2",
    "id": 24759045,
    "createTimeStamp": "2022-08-01T17:05:51.755Z"
  },
  "note": "test1",
  "id": 24759045,
  "createTimeStamp": "2022-08-01T17:05:36.750Z"
}

Created new js array -->

var result = [];

Code -->

for (var i in obj)
  result.push(obj[i]);

Find the solution from link below as well -->:):)

https://jsfiddle.net/kavinduxo/95qnpaed/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM