Find the differences between two arrays and eliminate any matching items

In my Vue project, I am working with two arrays. The first array, TempId, contains the following data:

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

The second array is called multiprices and it looks like this:

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

My goal is to identify and remove certain items from the second array and transfer them to a new dataset. In this example, I specifically need to move the item with id 46.

Answer №1

To narrow down the second array, you can use a filter based on whether the element is found in the id list of the 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)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method for shifting content as the window is resized to ensure it remains in its original position?

My page features a grid with three div elements. Each div is the size of the viewport, resulting in only one div being visible at a time, while the other two remain outside the view. This makes the grid three times larger than the viewport. When resizing ...

Deactivate button using Javascript

Can anyone assist me with this issue I am having? I currently have a button set up as follows: <input type="button" id="myButton" name="myButton" value="ClickMe!!" onClick="callMe()"/> I need to disable the button using jQuery, standard javascript ...

What could be causing the request body in ExpressJs to be blank?

Using ExpressJs, I have created a simple app that can handle post requests and form submissions. Below is the code snippet from my index.js file: require ('./models/db'); const express = require('express') const app = express() const bo ...

When experimenting with React JS, I attempted to trigger a modal to open upon clicking a card but unfortunately, the modal did not appear as expected

I'm currently facing an issue where I want to open a modal when clicking on a card. My setup involves using a button with a card component nested underneath it. import React from 'react'; import ReactDOM from 'react-dom'; import & ...

Create a unique type in Typescript that represents a file name with its corresponding extension

Is there a way for me to specify the type of a filename field in my object? The file name will consist of a string representing the name of the uploaded file along with its extension. For instance: { icon: "my_icon.svg" } I am looking for a ...

What is the most effective approach for handling JSON data from URLs in a professional manner?

Hey there, I'm facing a dilemma on how to efficiently handle data retrieved from various URLs on my website. My website allows me to fetch JSON data from multiple URLs. For instance, there's a page full of posts related to a specific group with ...

What is the best way to generate a dynamically interpolated string in JavaScript?

I'm currently developing a reusable UI component and am exploring options to allow the user of this component to provide their own template for a specific section within it. Utilizing TypeScript, I have been experimenting with string interpolation as ...

Looking for a way to dynamically append a child element within another child

Struggling to include a new child within a specific child in Json myObject:any[] = []; this.myObject = { "type": "object", "properties": { "first_name": { "type": "string" }, "last_name": { "type": "string" }, } } addF ...

Having trouble loading JSON data into HTML using jQuery

I'm struggling to create a basic menu using data from a JSON file. I've been attempting to figure out the correct logic for days now, but I seem to have hit a dead end: Here's the approach I've taken: $.ajax({ url: 'data ...

Javascript and iframes may encounter compatibility issues with browsers other than Internet Explorer

My HTML file includes an iframe and JavaScript that function correctly in IE Browser, but they do not work in other browsers such as Safari and Firefox. When using Safari, only the iframe box is displayed without showing the content inside it. I am curio ...

JQuery form plugin - submitting a form automatically on onchange event

As a beginner in JQuery, I am looking to utilize the JQuery form plugin for file uploads and would like to trigger the form submission as soon as a file is selected. This should occur upon the onchange event of the input tag: myfile. <html> <body ...

What could be causing my console to display "NaN" when working with the class extension feature

During the development of my project, I encountered a problem with getting the parameter from the parent class. The value returned is NaN while the other one returns true. Here is the code snippet: class transportasi {//class parent constructor(nama ...

Ways to insert data into a JavaScript array using PHP

I am trying to create a form that adds products to the cart: <form method="post" > <input type="hidden" value="<?php echo $product['id']?>" id="productId" name="productId"> <input type="hidden" value="<?php echo $ ...

Limiting the quantity in SimpleCart (js)

Currently, I am working on a WordPress website where I am using simpleCart(js) to add a shopping cart feature. I sell unique articles and do not require users to add more than one article to their cart. My main concern is how to prevent users from adding ...

What is the best way to utilize a php array as a pathway to access a specific value within another array?

Is there a way to access a specific array property by using another array as the path? The problem is that the property could be at any depth within the array structure. Let's illustrate this with an example... Consider the following associative arra ...

Improving mongo information using angularjs

Have an Angular and MongoDB application. This is a part of my API where I have POST and PUT requests. The POST request works fine, but when I send a PUT request, I get an error "Cannot set property 'typelocal' of undefined". However, the PUT requ ...

Use an array to store nested JSON fields

I'm currently seeking to enhance my proficiency in utilizing JavasScript, React, and Material-UI. I am faced with the challenge of sorting my table using a nested JSON structure and I am encountering difficulties with assigning the JSON fields to my a ...

Retrieve the specified columns as per user-defined filters

Being completely new to JavaScript, I have been assigned the task at hand. The technology stack includes node.js, express, mongodb, and mongoose. Imagine that the database/collection(s) consist of 1000 rows and each row has 50 columns. Some columns may h ...

Transmit precise data through socket.io to the client using socket.send

Is there a way to send and retrieve a specific variable when using socket.send? I need to send arrays of text as separate variables in my project using node.js to communicate data to the client-side (html). I understand that something is not quite right w ...

Encountering a 500 error when uploading images with Vue.js using Laravel 8 API

Here is the code snippet for uploading images and passing data to an API. <div class="form-group"> <div class="form-row"> <div class="col-md-6"> <input @change="onFileSelected& ...