Use object property values of two JS array of objects

array of objects javascript
javascript object get value by key
how to access array of objects in javascript
add property to object javascript
javascript object properties
javascript object keys
javascript add object to another object
javascript object methods

I have two arrays

let arr1 = [{'id': 'ee', 'seat': '12'}, 
  {'id': 'aa', 'seat': '8'}
let arr2 = [
  {'id': 's22', 'num': ''}, 
  {'id': '2s2', 'num': ''}

I want to copy seat values from arr1 to num property at arr2, but I only get last arr1 seat value in for loop.

for( let i = 0; i <= arr1.length; i++) {
  for( let x = 0; x <= arr2.length; x++) {
    arr2[x]['num'] = arr1[i]['seat'];


Iterate arr2 with Array.forEach(), and take the respective seat value by index from arr1:

const arr1 = [{'id': 'ee', 'seat': '12'},{'id': 'aa', 'seat': '8'}]
const arr2 = [{'id': 's22', 'num': ''},{'id': '2s2', 'num': ''}]

arr2.forEach((o, i) => o.num = arr1[i].seat)


Working with objects, This chapter describes how to use objects, properties, functions, and methods, and Object properties are basically the same as ordinary JavaScript variables, except for arrays, since each property is associated with a string value that can be used to access it. Alternatively, you can create an object with these two steps:. The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a forin loop. (The only difference is that a forin loop enumerates properties in the prototype chain as well.)

You need just a single loop and check if the index of the array if is (only) smaller than the length of the minimum of both arrays.

If the index get the length of an array, the access returns undefined, because this element is not in the array.

A further access to a property of this throws an error:

Unable to get property 'seat' of undefined or null reference

var arr1 = [{ id: 'ee', seat: '12' }, { id: 'aa', seat: '8' }],
    arr2 = [{ id: 's22', num: '' }, { id: '2s2', num: '' }],
    i, l;

for (i = 0, l = Math.min(arr1.length, arr2.length); i < l; i++) {
    arr2[i].num = arr1[i].seat;


Object.values( ) In JavaScript, Constructors are of two types in JavaScript i.e. built-in constructors(array and object) Objects defined using an object constructor are then made instants using the Object.values() is used for returning enumerable property values of an array  Object.values() is used for returning enumerable property values of a simple array. Object.values() is used for returning enumerable property values of an array like object. Object.values() is used for returning enumerable property values of an array like object with random key ordering.

You can do it in just one for loop.

for(let i = 0; i < arr1.length; i++) {
    arr2[i].num = arr1[i].seat;

Hope this helps!

Objects, An empty object (“empty cabinet”) can be created using one of two syntaxes: let user = new Property values are accessible using the dot notation: // get property Objects are associative arrays with several special features. I looked over in SO and was able to find answer which is filtered based on one property (Create array of unique objects by property), but couldn't find which could do based on 2 properties. javascript arrays

Assuming you want to match indices, this should do it.

const arr1 = [
  {'id': 'ee', 'seat': '12'}, 
  {'id': 'aa', 'seat': '8'}
const arr2 = [
  {'id': 's22', 'num': ''}, 
  {'id': '2s2', 'num': ''}

const result =, i) => ({...e, ...{num: arr1[i].seat}}))


How can I merge properties of two JavaScript objects dynamically , If you have values that are arrays , then use "arrayMerge" option of github Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1 * @​param If you want to create a new object without modifying either argument, use this: Recursively merge properties of two objects */ function MergeRecursive(obj1,  const sumValues = (obj) => Object.keys(obj).reduce((acc, value) => acc + obj[value], 0); We're making it an immutable function while we're at it. What reduce is doing here is simply this: Start with a value of 0 for the accumulator, and add the value of the current looped item to it.

Useful Javascript Array and Object Methods, The returned value can be of any type (i.e. object, array, string, integer). There are some really cool use cases for .reduce() outlined in the MDN things likes flattening an array of arrays, grouping objects by a property, and object is created) and it can also be used to combine multiple objects together. JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.

Object Equality in JavaScript, You might suppose that if two objects have the same properties and all of their Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and Then you can use JavaScript's built-in equality operators​. You want to render this list, but first you want to order it by the value of one of the properties. For example you want to order it by the color name, in alphabetical order: black, red, white. You can use the sort() method of Array, which takes a callback function, which takes as parameters 2 objects contained in the array (which we call a and b):

JavaScript Objects, But objects can contain many values. JavaScript objects are containers for named values called properties or An object definition can span multiple lines:​  An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects.

  • You shouldn't use nested loops, currently you begin with setting every num of your second array to the first seat of the first array, then carry on setting every num of your second array to the second seat of the first array. You haven't described what the association between the first array and second array elements should be, so it's not currently possible to provide you with a specific answer.