score:1

you are on a right way, but you are not using array.filter properly.

and also the provided array of object is not also in a good format. object should be in a key:value pairs.

state.products = [
    { id: 1, name: "bottle" },
    { id: 2, name: "umbrella" },
    { id: 3, name: "shoe" }
]

const getallarrayexcept = (id) => {
    // this will return all the array except provided id
    return state.products.filter((s) => s.id !== id)
}

const getonlyarray = (id) => {
    // this will return only item which match the provided id
    return state.products.filter((s) => s.id === id)
}

console.log(getallarrayexcept(1))
/*
output: [{ id = 2, name = umbrella }, { id = 3, name = shoe }]
*/
console.log(getonlyarray(1))
/*
output: [{ id = 1, name = bottle }]
*/

here is the working snippet:

const products = [
    { id: 1, name: "bottle" },
    { id: 2, name: "umbrella" },
    { id: 3, name: "shoe" }
]

const getallarrayexcept = (id) => {
    // this will return all the array except provided id
    return products.filter((s) => s.id !== id)
}

const getonlyarray = (id) => {
    // this will return only item which match the provided id
    return products.filter((s) => s.id === id)
}

console.log("all array except: ", getallarrayexcept(1))
console.log("only provided item in array: ", getonlyarray(1))


Related Query

More Query from same tag