0

I have JSON data array of

[
  {
    _id: 5f2084f656dc2428e96fbe92,
    category: [ '5e5150c6c52a3904b74d6ff7', null ],
    coordinates: { lat: 37.8025909, lng: -122.4336412 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3323.833296768243
  },
  {
    _id: 5f2084fb56dc2428e9703f3b,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.7683708, lng: -122.4564649 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3340.838606150309
  },
  {
    _id: 5f2084f656dc2428e96fbe86,
    category: [ '5e5150c6c52a3904b74d6ff7', null ],
    coordinates: { lat: 37.7669182, lng: -122.4561008 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3349.000719541262
  },
  {
    _id: 5f2084fb56dc2428e9703f3f,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.7669182, lng: -122.4561008 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3349.000719541262
  },
  {
    _id: 5f2084fa56dc2428e9702130,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.7669182, lng: -122.4561008 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3349.000719541262
  },
  {
    _id: 5f2084fb56dc2428e9703ee5,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.7787325, lng: -122.4572683 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3357.3418413767595
  },
  {
    _id: 5f2084f856dc2428e96ff091,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.8049528, lng: -122.4249378 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3377.281713590449
  },
  {
    _id: 5f2084fb56dc2428e9703f73,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.8049528, lng: -122.4249378 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3377.281713590449
  },
  {
    _id: 5f2084fb56dc2428e9703f39,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.7788351, lng: -122.4575644 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3384.6341862170793
  },
  {
    _id: 5f14aa92745456119cf27701,
    category: [ '5e5150f0c52a3904b74d6ffb', null ],
    coordinates: { lat: 37.779187, lng: -122.4577491 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3405.9730381738473
  },
  {
    _id: 5f2084fb56dc2428e9703f21,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.8034776, lng: -122.4027084 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3501.3565554464535
  },
]

I want to find an object that has a category on of 'favTypes' that returns

"5e5150bcc52a3904b74d6ff6,5e5150c6c52a3904b74d6ff7,5e5150d6c52a3904b74d6ff8"

Searching objects that have one of this three object_Id in their category. I have tried

jsondatas.filter(jsondata => word.category = favTypes);

However, it is returning the jsondatas not filtered objects. Is there any way that I can filter

1
  • Filter will return the object(json) which satisfies the match condition. What do you need exactly? Commented Jul 30, 2020 at 5:41

3 Answers 3

1

.indexOf() on each iterating as array or the whole as a string indexOf(), .includes() or .match()

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

Comments

0

You have to use "==" instead of "=".

"jsondatas.filter(jsondata => word.category == favTypes)"

Comments

0

You can use nested foreach and add some condition, then push it to the initial array. See the code below

const arr = [
  {
    _id: "5f2084f656dc2428e96fbe92",
    category: ["5e5150c6c52a3904b74d6ff7", null],
    coordinates: { lat: 37.8025909, lng: -122.4336412 },
    location: { type: "Point", coordinates: [Array] },
    calcDistance: 3323.833296768243,
  },
  {
    _id: "5f2084fb56dc2428e9703f3b",
    category: ["5e5150bcc52a3904b74d6ff6", null],
    coordinates: { lat: 37.7683708, lng: -122.4564649 },
    location: { type: "Point", coordinates: [Array] },
    calcDistance: 3340.838606150309,
  },
  {
    _id: "5f2084f656dc2428e96fbe86",
    category: ["5e5150c6c52a3904b74d6ff7", null],
    coordinates: { lat: 37.7669182, lng: -122.4561008 },
    location: { type: "Point", coordinates: [Array] },
    calcDistance: 3349.000719541262,
  },
  {
    _id: "5f2084fb56dc2428e9703f3f",
    category: ["5e5150bcc52a3904b74d6ff6", null],
    coordinates: { lat: 37.7669182, lng: -122.4561008 },
    location: { type: "Point", coordinates: [Array] },
    calcDistance: 3349.000719541262,
  },
  {
    _id: "5f2084fa56dc2428e9702130",
    category: ["5e5150bcc52a3904b74d6ff6", null],
    coordinates: { lat: 37.7669182, lng: -122.4561008 },
    location: { type: "Point", coordinates: [Array] },
    calcDistance: 3349.000719541262,
  },
  {
    _id: "5f2084fb56dc2428e9703ee5",
    category: ["5e5150bcc52a3904b74d6ff6", null],
    coordinates: { lat: 37.7787325, lng: -122.4572683 },
    location: { type: "Point", coordinates: [Array] },
    calcDistance: 3357.3418413767595,
  },
  {
    _id: "5f2084f856dc2428e96ff091",
    category: ["5e5150bcc52a3904b74d6ff6", null],
    coordinates: { lat: 37.8049528, lng: -122.4249378 },
    location: { type: "Point", coordinates: [Array] },
    calcDistance: 3377.281713590449,
  },
  {
    _id: "5f2084fb56dc2428e9703f73",
    category: ["5e5150bcc52a3904b74d6ff6", null],
    coordinates: { lat: 37.8049528, lng: -122.4249378 },
    location: { type: "Point", coordinates: [Array] },
    calcDistance: 3377.281713590449,
  },
  {
    _id: "5f2084fb56dc2428e9703f39",
    category: ["5e5150bcc52a3904b74d6ff6", null],
    coordinates: { lat: 37.7788351, lng: -122.4575644 },
    location: { type: "Point", coordinates: [Array] },
    calcDistance: 3384.6341862170793,
  },
  {
    _id: "5f14aa92745456119cf27701",
    category: ["5e5150f0c52a3904b74d6ffb", null],
    coordinates: { lat: 37.779187, lng: -122.4577491 },
    location: { type: "Point", coordinates: [Array] },
    calcDistance: 3405.9730381738473,
  },
  {
    _id: "5f2084fb56dc2428e9703f21",
    category: ["5e5150bcc52a3904b74d6ff6", null],
    coordinates: { lat: 37.8034776, lng: -122.4027084 },
    location: { type: "Point", coordinates: [Array] },
    calcDistance: 3501.3565554464535,
  },
];

const arrayToFilter = [
  "5e5150bcc52a3904b74d6ff6",
  "5e5150c6c52a3904b74d6ff7",
  "5e5150d6c52a3904b74d6ff8",
];

//initialize new array
const newArr = [];

arr.forEach((x) => {
  arrayToFilter.forEach((y) => {
    if (x.category[0] === y) {
      //push the item to the initialized
      //if the key of the item is matched
      newArr.push(x);
    }
  });
});

//you can use this also
//arr.forEach((x) =>
//  arrayToFilter.forEach((y) => x.category[0] === y && newArr.push(x))
//);

console.log(newArr);

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.