0

Compare all array item into JSON array object

const filterArray = ['Ford', 'Fiat'];

const showrooms = [{
    "Name": "Mark Auto",
    "Location": "Delhi",
    "Cars": ['Ford', 'BMW', 'Fiat']
  },
  {
    "Name": "Cardekho",
    "Location": "Mumbai",
    "Cars": ['Ford', 'Fiat']
  },
  {
    "Name": "Tata Arena",
    "Location": "Pune",
    "Cars": ['Ford', 'BMW']
  },
  {
    "Name": "Nexa Showroom",
    "Location": "Noida",
    "Cars": ['Suzuki', 'Ford', 'Tata', 'Fiat']
  }
]

I write code to filter data but not working.

const result = this.showrooms.filter(c=> this.filterArray.includes(c.Cars)).map(a=>a.Name);

Desired Output

['Mark Auto', 'Cardekho', 'Nexa Showroom']

7 Answers 7

2

You could use every to check if every showroom's cars include all of the cars in the filterArray

const filterArray = ['Ford', 'Fiat'];

const showrooms = [
  {
    Name: "Mark Auto",
    Location: "Delhi",
    Cars: ["Ford", "BMW", "Fiat"],
  },
  {
    Name: "Cardekho",
    Location: "Mumbai",
    Cars: ["Ford", "Fiat"],
  },
  {
    Name: "Tata Arena",
    Location: "Pune",
    Cars: ["Ford", "BMW"],
  },
  {
    Name: "Nexa Showroom",
    Location: "Noida",
    Cars: ["Suzuki", "Ford", "Tata", "Fiat"],
  },
];

const res = showrooms
  .filter((s) => filterArray.every((c) => s.Cars.includes(c)))
  .map((s) => s.Name);

console.log(res);

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

Comments

1

Use every -

const filterArray = ['Ford', 'Fiat'];

const showrooms = [{
    "Name": "Mark Auto",
    "Location": "Delhi",
    "Cars": ['Ford', 'BMW', 'Fiat']
  },
  {
    "Name": "Cardekho",
    "Location": "Mumbai",
    "Cars": ['Ford', 'Fiat']
  },
  {
    "Name": "Tata Arena",
    "Location": "Pune",
    "Cars": ['Ford', 'BMW']
  },
  {
    "Name": "Nexa Showroom",
    "Location": "Noida",
    "Cars": ['Suzuki', 'Ford', 'Tata', 'Fiat']
  }
];
const result = showrooms.filter(c => filterArray.every(e => c.Cars.includes(e))).map(a => a.Name);
console.log(result);

Comments

1

Just need update the condition inside you filter. The current doesn't return as your expected result.

const filterArray = ["Ford", "Fiat"];

const showrooms = [
  {
    Name: "Mark Auto",
    Location: "Delhi",
    Cars: ["Ford", "BMW", "Fiat"],
  },
  {
    Name: "Cardekho",
    Location: "Mumbai",
    Cars: ["Ford", "Fiat"],
  },
  {
    Name: "Tata Arena",
    Location: "Pune",
    Cars: ["Ford", "BMW"],
  },
  {
    Name: "Nexa Showroom",
    Location: "Noida",
    Cars: ["Suzuki", "Ford", "Tata", "Fiat"],
  },
];

const result = showrooms
  .filter((c) => {
     let existedInArray = true
     
     for(let i=0; i < filterArray.length; i++){
      if(!c.Cars.includes(filterArray[i])) existedInArray = false
     }
     return existedInArray
  })
  .map((a) => a.Name);
  
 console.log(result)

Comments

0
const filterArray = ["Ford", "Fiat"];

const showrooms = [
  {
    Name: "Mark Auto",
    Location: "Delhi",
    Cars: ["Ford", "BMW", "Fiat"],
  },
  {
    Name: "Cardekho",
    Location: "Mumbai",
    Cars: ["Ford", "Fiat"],
  },
  {
    Name: "Tata Arena",
    Location: "Pune",
    Cars: ["Ford", "BMW"],
  },
  {
    Name: "Nexa Showroom",
    Location: "Noida",
    Cars: ["Suzuki", "Ford", "Tata", "Fiat"],
  },
];

const result = showrooms
  .filter((c) => {
     let existedInArray = true
     
     for(let i=0; i < filterArray.length; i++){
      if(!c.Cars.includes(filterArray[i])) existedInArray = false
     }
     return existedInArray
  })
  .map((a) => a.Name);
  
 console.log(result)

I think This Will Works

Comments

0

You could use reduce + every to solve this in one loop. When you do something combining filter and map, reduce is really a good way.

const filterArray = ['Ford', 'Fiat'];
const showrooms = [{
    "Name": "Mark Auto",
    "Location": "Delhi",
    "Cars": ['Ford', 'BMW', 'Fiat']
  },
  {
    "Name": "Cardekho",
    "Location": "Mumbai",
    "Cars": ['Ford', 'Fiat']
  },
  {
    "Name": "Tata Arena",
    "Location": "Pune",
    "Cars": ['Ford', 'BMW']
  },
  {
    "Name": "Nexa Showroom",
    "Location": "Noida",
    "Cars": ['Suzuki', 'Ford', 'Tata', 'Fiat']
  }
]


const result = showrooms.reduce((prev, curr) => {
  const isFilterCarsIncluded = filterArray.every(car => curr.Cars.includes(car));

  if (isFilterCarsIncluded) {
    return [...prev, curr.Name];
  }

  return prev;
}, []);

console.info(result);

Comments

0

Demo:

showrooms
  .filter(showroom => filterArray.every(item => showroom.Cars.includes(item)))
  .map(showroom => showroom.Name);

Done.

Comments

0

with reduce in one iteration, we can accumulate desired output

const combine = (arr, filters) =>
  arr.reduce((acc, { Name, Cars }) => {
    filters.every((car) => Cars.includes(car)) && acc.push(Name);
    return acc;
  }, []);

const filterArray = ["Ford", "Fiat"];

const showrooms = [
  {
    Name: "Mark Auto",
    Location: "Delhi",
    Cars: ["Ford", "BMW", "Fiat"],
  },
  {
    Name: "Cardekho",
    Location: "Mumbai",
    Cars: ["Ford", "Fiat"],
  },
  {
    Name: "Tata Arena",
    Location: "Pune",
    Cars: ["Ford", "BMW"],
  },
  {
    Name: "Nexa Showroom",
    Location: "Noida",
    Cars: ["Suzuki", "Ford", "Tata", "Fiat"],
  },
];

console.log(combine(showrooms, filterArray));

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.