简体   繁体   中英

Comparing two parameters of an object array in JavaScript

I am completing a homework assignment where I will have to filter an array into a new array for a vehicle with the term of “ford” in it. The assignment wants the format to be in ES6 using the arrow function syntax so something like

const arr = [
{name: “Honda”, type: “Accord”},
{name: “ford”, type: “fusion”},
{name: “Toyota”, type: “Camry”}
]

const newArr = [...arr].filter(value => value === ‘ford’);

Console.log(newArr);

I know this is incorrect and wouldn't actually get the name of the vehicle that has “ford” in it but I'm giving an example on how they would want it formatted.

You need value.name also check the quotes. In this case there may not be any use of the spread operator

 const arr = [{ name: 'Honda', type: 'Accord' }, { name: 'ford', type: 'fusion' }, { name: 'Toyota', type: 'Camry' } ] const newArr = arr.filter(value => value.name === 'ford'); console.log(newArr); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM