1

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.

2
  • So your code seems to be working. What is the question? Commented Aug 2, 2022 at 0:02
  • Object.keys(obj).filter((key) => !Number.isNaN(parseInt(key))).map((key) => obj[key])) Commented Aug 2, 2022 at 0:07

3 Answers 3

2

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]);
Sign up to request clarification or add additional context in comments.

Comments

0

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/

Comments

-1

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])
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.