Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js two array combining with id

var messages = [{userId: 2, content: "Salam"}, {userId: 5, content: "Hello"},{userId: 4, content: "Moi"}];
var users = [{id: 2, name: "Grace"}, {id: 4, name: "Janetta"},{id: 5, name: "Sara"}];

var messagesWithUserNames = messages.map((msg)=> {
  var haveEqualId = (user) => user.id === msg.userId
  var userWithEqualId= users.find(haveEqualId)
  return Object.assign({}, msg, userWithEqualId)
})
console.log(messagesWithUserNames)
Comment

js two array combining with id neasted

const a1 = [{ id : 1, name : "test"}, { id : 2, name : "test2"}]
const a2 = [{ id : 1, count : "1"}, {id : 2, count : "2"}]

const merge = (arr1, arr2) => {
  const temp = []

  arr1.forEach(x => {
    arr2.forEach(y => {
      if (x.id === y.id) {
        temp.push({ ...x, ...y })
      }
    })
  })

  return temp
}

console.log(merge(a1, a2))
Comment

PREVIOUS NEXT
Code Example
Javascript :: Highest Scoring Word 
Javascript :: Add Multilanguage Support to React App 
Javascript :: remove the last character from a string in JavaScript, 
Javascript :: dotenv in node js 
Javascript :: node js create pdf from html 
Javascript :: hook usePreloadImages 
Javascript :: Referrer Policy: strict-origin-when-cross-origin angular 
Javascript :: rename column infotable thingworx 
Javascript :: prototype in javascript class 
Javascript :: mongoose add document 
Javascript :: Working with Legacy Tables sequelize 
Javascript :: node express tutorial 
Javascript :: rating calculator formula javascript 
Javascript :: sorting algorithms in node.js 
Javascript :: lexical environment in javascript 
Javascript :: jquery add url parameter to link dynamically by class 
Javascript :: change bg-color all class 
Javascript :: javascript access pushed element 
Javascript :: jquery onchange event 
Javascript :: componentdidmount react hooks 
Javascript :: string to object js 
Javascript :: react sticky hook 
Javascript :: get random elements from array javascript 
Javascript :: Object.create Polyfill 
Javascript :: google places autocomplete react native 
Javascript :: //Splice remove and add new elements in an array in javascript 
Javascript :: jquery clone object 
Javascript :: javascript dom manipulation 
Javascript :: replace in string all occurance jquery 
Javascript :: Backbone Router 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =