Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

match ids from 2 arrays in javascript asynchronous programming

var result1 = [
    {id:1, name:'Sandra', type:'user', username:'sandra'},
    {id:2, name:'John', type:'admin', username:'johnny2'},
    {id:3, name:'Peter', type:'user', username:'pete'},
    {id:4, name:'Bobby', type:'user', username:'be_bob'}
];

var result2 = [
    {id:2, name:'John', email:'johnny@example.com'},
    {id:4, name:'Bobby', email:'bobby@example.com'}
];

var props = ['id', 'name'];

var result = result1.filter(function(o1){
    // filter out (!) items in result2
    return !result2.some(function(o2){
        return o1.id === o2.id;          // assumes unique id
    });
}).map(function(o){
    // use reduce to make objects with only the required properties
    // and map to apply this to the filtered array as a whole
    return props.reduce(function(newo, name){
        newo[name] = o[name];
        return newo;
    }, {});
});

document.body.innerHTML = '<pre>' + JSON.stringify(result, null, 4) +
        '</pre>';
Comment

PREVIOUS NEXT
Code Example
Javascript :: img tag in react 
Javascript :: return an object from an array javascript 
Javascript :: material ui dark theme 
Javascript :: js push in object 
Javascript :: disable button in angular 
Javascript :: js concat string 
Javascript :: javascript get form input data 
Javascript :: react onclick runs on load 
Javascript :: inline focus style 
Javascript :: dropdown search field in react native 
Javascript :: Find the Missing Number js 
Javascript :: compare two arrays and remove duplicates javascript 
Javascript :: express cors specific origins 
Javascript :: how to dynamic title in nuxt 
Javascript :: launch uikit modal from php 
Javascript :: if browsertab is active jquery 
Javascript :: js binary search 
Javascript :: reset form jquery 
Javascript :: axios get method 
Javascript :: joi validation enum 
Javascript :: how to pass state from one component to another in functional component 
Javascript :: get an element from outside of iframe jquery 
Javascript :: discord buttons 
Javascript :: alert 
Javascript :: javascript cancel scroll 
Javascript :: can we call ajax inside ajax success 
Javascript :: javascript convert number to spreadsheet column 
Javascript :: express reload page after post 
Javascript :: input events 
Javascript :: angular conditionally show tooltip 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =