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:'[email protected]'},
{id:4, name:'Bobby', email:'[email protected]'}
];
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>';
Also in JavaScript:
- cannot get / angular
- how to make a translator in python
- convert table to excel reactjs
- require jquery tampermonkey
- Can't find Node.js binary "node": path does not exist. Make sure Node.js is installed and in your PATH, or set the "runtimeExecutable" in your launch.json
- get radio button value javascript
- jquery ajax on fail
- check is array javascript
- javascript get last element of array pull
- how to get the square root in js
- javascript define global variable
- google docs api word count
- js date minus 18 years
- bootstrap 3 min max price range slider
- if typeof equals array javascript
- javascript genreate number id
- async await anonymous function
- es6 array sum javascript
- js remove property from object
- JavaScript after
- javascript javascript
- angular mouseenter
- add dev dependency yarn
- dangerouslySetInnerHTML did not match error in React