-2

in vue i have an array with this data(TempId):

[{"id":47},{"id":45},{"id":48}]

and second array with this:

 multiprices: [
    {
        id: 45,
        name: "",
        price: "2600000",
    },
    {
        id: 46,
        name: "",
        price: "2600000",
    },
    {
        id: 47,
        name: "",
        price: "2600000",
    },
    {
        id: 48,
        name: "",
        price: "2600000",
    }
],

i want to find and remove the items of second array and copy them to new data.(in this example just id 46)

1 Answers1

0

You could filter the second array with the condition of element is in the id list of first array

const tempObj = [{ id: 47 }, { id: 45 }, { id: 48 }]
const tempId = tempObj.map(({ id }) => id)

const multiprices = [
  {
    id: 45,
    name: "",
    price: "2600000",
  },
  {
    id: 46,
    name: "",
    price: "2600000",
  },
  {
    id: 47,
    name: "",
    price: "2600000",
  },
  {
    id: 48,
    name: "",
    price: "2600000",
  },
]

const res = multiprices.filter((multiprice) => !tempId.includes(multiprice.id))

console.log(res)
hgb123
  • 9,840
  • 3
  • 11
  • 31