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 :: json decode list flutter 
Javascript :: change key in array of objects javascript 
Javascript :: p5js cdn 
Javascript :: get combinations of two js 
Javascript :: install aos angular 10 
Javascript :: how to change text in html using javascript 
Javascript :: call laravel route js 
Javascript :: dockerfile copy ignore node_modules 
Javascript :: import jquery into js file 
Javascript :: javascript remove item onclick 
Javascript :: date js 
Javascript :: nested loops javascript 
Javascript :: javascript regex check phone number 
Javascript :: how to add css in js 
Javascript :: how to add two attay into object in javascript 
Javascript :: sequelize dialect 
Javascript :: convert decimal to binary javascript 
Javascript :: window.location.search get parameters react 
Javascript :: how to get seconds in timstamps js 
Javascript :: for element in string js 
Javascript :: what called window.onerror 
Javascript :: alert and prompt in javascript 
Javascript :: convert cookies to json javascript 
Javascript :: js export multiple functions 
Javascript :: getting current date and time in javascript 
Javascript :: javascript htmlentities 
Javascript :: convert date time to date function javascript 
Javascript :: html parser javascript 
Javascript :: how to define state in react function 
Javascript :: react useref 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =