0

Given an array of json object like this below, (the json object such as "name2" and "name4" will definitely have only one key-value)

[
  {
    abc: 123,
    id: '18263322',
    name: 'name1'
  },
  { name: 'name2' },
  {
    abc: 456,
    id: '18421634',
    name: 'name3'
  },
  { name: 'name4' }
]

How can I subset this so that I have two array of json objects:

[
  {
    abc: 123,
    id: '18263322',
    name: 'name1'
  },
  {
    abc: 456,
    id: '18421634',
    name: 'name3'
  }
]

and

[
  { name: 'name2' },
  { name: 'name4' }
]
4
  • What do you want exactly? Can you explain more your question? Thank Commented Jul 19, 2021 at 0:05
  • use Array.prototype.reduce Commented Jul 19, 2021 at 0:12
  • I would like to split the first array (in the first code section) in to two array of json. I use this code: data = [ { abc: 123, id: '18263322', name: 'name1' }, { name: 'name2' }, { abc: 456, id: '18421634', name: 'name3' }, { name: 'name4' } ] data.filter(function(x) { console.log(Object.keys(x).length !== 1); }); This gets me true and false but not the array itself, and it seems very slow to process Commented Jul 19, 2021 at 0:12
  • @SunTianyi Since you have tried so you should include it in the question itself. So that other people can understand what have you tried Commented Jul 19, 2021 at 0:16

1 Answer 1

1

You can use reduce here

const arr = [
  {
    abc: 123,
    id: "18263322",
    name: "name1",
  },
  { name: "name2" },
  {
    abc: 456,
    id: "18421634",
    name: "name3",
  },
  { name: "name4" },
];

const [single, multiple] = arr.reduce((acc, curr) => {
    Object.keys(curr).length === 1 ? acc[0].push(curr) : acc[1].push(curr);
    return acc;
  },[[], []]
);

console.log(single);
console.log(multiple);

You can also do something like

const [single, multiple] = arr.reduce((acc, curr) => {
    acc[Object.keys(curr).length === 1 ? 0 : 1].push(curr);
    return acc;
  },[[], []]);

using filter

const arr = [
  {
    abc: 123,
    id: "18263322",
    name: "name1",
  },
  { name: "name2" },
  {
    abc: 456,
    id: "18421634",
    name: "name3",
  },
  { name: "name4" },
];

const single = arr.filter((o) => Object.keys(o).length === 1);
const multiple = arr.filter((o) => Object.keys(o).length !== 1);

console.log(single);
console.log(multiple);

Sign up to request clarification or add additional context in comments.

5 Comments

That works like a charm! I thought about Object.keys(x).length but never thought about reduce. Thank you so much!
@SunTianyi Added solution with filter also. Have a look
Do you mind commenting on which one is more efficient? I think the reduce is more efficient because it seems like only looping through once? whereas the second method does two filters loops @decpk
You shouldn't consider the efficient solution if the dataset is small. If it would have millions of records then I would go with reduce since it takes a single loop to filter the data.
Thanks for your advise! I was asking the efficiency because I have maybe ten thousands of records.

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.