0

I have a JSON file with data inside. I have to filter the data by : if user has more than two names, and if user ids are consecutive. The JSON file :

[
{
"_id": "62bd5fba34a8f1c90303055c",
"index": 0,
"email": "[email protected]",
"nameList": [
  {
    "id": 0,
    "name": "Wendi Mooney"
  },
  {
    "id": 2,
    "name": "Holloway Whitehead"
  }
]
},
{
"_id": "62bd5fbac3e5a4fca5e85e81",
"index": 1,
"nameList": [
  {
    "id": 0,
    "name": "Janine Barrett"
  },
  {
    "id": 1,
    "name": "Odonnell Savage"
  },
  {
    "id": 2,
    "name": "Patty Owen"
  }
]
}, ...

I have managed to find an a solution to filter if the users have more than two names : userData.filter((names,i) => { return names?.nameList?.filter(names => { return names.name;}).length > 2 ; }) But I cant seem to grasp myself around the concept of filtering the consecutive ids. Also I was advised to not use any for loops at all. Only ES6 array loops like map, forEach and filter.

5
  • What is the expected output? Commented Jul 6, 2022 at 10:01
  • Output all the objects that meet the criteria : More than two users and its ids are consecutive. Commented Jul 6, 2022 at 10:03
  • so, some objects in nameList can have id but no name? Commented Jul 6, 2022 at 10:03
  • Pretty much not, all objects in nameList have and id and a name. Commented Jul 6, 2022 at 10:05
  • if you can find a function to check if an array has consecutive numbers then you have almost solved it stackoverflow.com/questions/34257152/… . then inside the filter add a && condition with isConsecutive(namesList.map(n =>n.id)) Commented Jul 6, 2022 at 10:06

1 Answer 1

2

Here's a solution that uses every() to compare the ID of every element with the previous ID:

const result = data.filter(({nameList}) =>
  nameList.length > 2 &&
  nameList.every(({id}, i, a) => !i || id === a[i - 1].id + 1)
);

Complete snippet:

const data = [{
  "_id": "62bd5fba34a8f1c90303055c",
  "index": 0,
  "email": "[email protected]",
  "nameList": [{
    "id": 0,
    "name": "Wendi Mooney"
  }, {
    "id": 2,
    "name": "Holloway Whitehead"
  }]
}, {
  "_id": "62bd5fbac3e5a4fca5e85e81",
  "index": 1,
  "nameList": [{
    "id": 0,
    "name": "Janine Barrett"
  }, {
    "id": 1,
    "name": "Odonnell Savage"
  }, {
    "id": 2,
    "name": "Patty Owen"
  }]
}];

const result = data.filter(({nameList}) =>
  nameList.length > 2 &&
  nameList.every(({id}, i, a) => !i || id === a[i - 1].id + 1)
);

console.log(result);

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

3 Comments

Thanks, seems to work perfectly. Also one of my staff did suggest using .some() and it matches it perfectly!
Updated to use every(). Feels better than !some().
was just about to suggest every ... but I almost posted a similar answer, like nameList.length > 2 && nameList.slice(1).every(({ id }, index) => id === nameList[index].id + 1)

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.