2

Here is my sample data

const data = [{"amount": "600,000", "cover": null, "id": "1", "img": "636e56de36301.1.png", "make": "bmw", "model": "bmw", "name": "APA", "policy": "Motor Insurance", "rate": "6"}, {"amount": "300,000", "cover": null, "id": "2", "img": "63723574a81ce.1.png", "make": "ferrari", "model": "ferrari", "name": "CIC", "policy": "Motor Insurance", "rate": "3"}, {"amount": "450,000", "cover": null, "id": "3", "img": "63723726cb1df.1.png", "make": "audi", "model": "audi", "name": "Mayfair Insurance", "policy": "Motor Insurance", "rate": "4.5"}]

and here is the code am using to return the array for id 3.

const data = [{"amount": "600,000", "cover": null, "id": "1", "img": "636e56de36301.1.png", "make": "bmw", "model": "bmw", "name": "APA", "policy": "Motor Insurance", "rate": "6"}, {"amount": "300,000", "cover": null, "id": "2", "img": "63723574a81ce.1.png", "make": "ferrari", "model": "ferrari", "name": "CIC", "policy": "Motor Insurance", "rate": "3"}, {"amount": "450,000", "cover": null, "id": "3", "img": "63723726cb1df.1.png", "make": "audi", "model": "audi", "name": "Mayfair Insurance", "policy": "Motor Insurance", "rate": "4.5"}]

const id = ['3']

const provider = data.reduce((prv, item) => {
    if(id.includes(item.id)){
      return prv 
    }
    return prv
 });
console.log('This is provider' ,provider);

Unfortunately, the return am getting is data with an id of 1

Output:

 This is provider {"amount": "600,000", "cover": null, "id": "1", "img": "636e56de36301.1.png", "make": "bmw", "model": "bmw", "name": "APA", "policy": "Motor Insurance", "rate": "6"}

can someone tell what am doing wrong please

1
  • Could you please clarify what data you would like to return ? A single array of data yes, but with what data in it ? Commented Dec 3, 2022 at 10:30

2 Answers 2

2

If your intention is to return only an object you can use reduce() but currently, you're doing it a little bit wrong way.

  1. You need to pass an initial value to the reduce() if it's not then the reduce picks the first element of the array as the initial value and starts iterating from the second element. so when condition(s) are false it returns the first element of the array. so you need to be careful with it.
  2. You need to return item here if(id.includes(item.id)) return item not prev.

Here is the solution with .reduce()

const data = [{"amount": "600,000", "cover": null, "id": "1", "img": "636e56de36301.1.png", "make": "bmw", "model": "bmw", "name": "APA", "policy": "Motor Insurance", "rate": "6"}, {"amount": "300,000", "cover": null, "id": "2", "img": "63723574a81ce.1.png", "make": "ferrari", "model": "ferrari", "name": "CIC", "policy": "Motor Insurance", "rate": "3"}, {"amount": "450,000", "cover": null, "id": "3", "img": "63723726cb1df.1.png", "make": "audi", "model": "audi", "name": "Mayfair Insurance", "policy": "Motor Insurance", "rate": "4.5"}]

const id = ['3'];
const provider = data.reduce((prv, item) => {
  if(id.includes(item.id)){
    return item 
  }
return prv
}, {});

console.log(provider);

Also You can use .filter()

const data = [{"amount": "600,000", "cover": null, "id": "1", "img": "636e56de36301.1.png", "make": "bmw", "model": "bmw", "name": "APA", "policy": "Motor Insurance", "rate": "6"}, {"amount": "300,000", "cover": null, "id": "2", "img": "63723574a81ce.1.png", "make": "ferrari", "model": "ferrari", "name": "CIC", "policy": "Motor Insurance", "rate": "3"}, {"amount": "450,000", "cover": null, "id": "3", "img": "63723726cb1df.1.png", "make": "audi", "model": "audi", "name": "Mayfair Insurance", "policy": "Motor Insurance", "rate": "4.5"}]

const id = ['3'];
console.log(data.filter(it => id.includes(it.id)));

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

Comments

0

It looks like your code is not working because you're using the Array.reduce() method incorrectly. The reduce() method is used to reduce an array to a single value, not to filter out values from the array.

To filter out the data with the id of 3, you can use the Array.filter() method instead. Here's how your code would look with the filter() method:

const data = [
  {
    "amount": "600,000",
    "cover": null,
    "id": "1",
    "img": "636e56de36301.1.png",
    "make": "bmw",
    "model": "bmw",
    "name": "APA",
    "policy": "Motor Insurance",
    "rate": "6"
  },
  {
    "amount": "300,000",
    "cover": null,
    "id": "2",
    "img": "63723574a81ce.1.png",
    "make": "ferrari",
    "model": "ferrari",
    "name": "CIC",
    "policy": "Motor Insurance",
    "rate": "3"
  },
  {
    "amount": "450,000",
    "cover": null,
    "id": "3",
    "img": "63723726cb1df.1.png",
    "make": "audi",
    "model": "audi",
    "name": "Mayfair Insurance",
    "policy": "Motor Insurance",
    "rate": "4.5"
  }
];

const id = ['3'];

const provider = data.filter(item => id.includes(item.id));
console.log('This is provider', provider);

This should output the following data:

[
  {
    "amount": "450,000",
    "cover": null,
    "id": "3",
    "img": "63723726cb1df.1.png",
    "make": "audi",
    "model": "audi",
    "name": "Mayfair Insurance",
    "policy": "Motor Insurance",
    "rate": "4.5"
  }
]

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.