Combine multiple arrays containing objects using object property values

Numbersgame

I have an array that contains multiple objects, which i'm trying to add to another larger array with more objects in it. I'm wondering how I can automatically assign their position based on a property that each object shares. Example:

function person(name, age) {
this.name = name;
this.age = age;
}

function pet(name, age) {
this.name = name;
this.age = age;
}

var person1 = new person("Amanda", 25);
var person2 = new person("Jack", 29);
var pet1 = new pet("Fluffy", 4);
var pet2 = new pet("Spaz", 5);

personArray = [person1, person2];
petArray = [pet1, pet2];

In the example I would like to combine the personArray and petArray into a new array using the age property of each to sort them into the array by order of youngest to oldest. Any help is appreciated.

Paul Roub

concat() and sort() will get you there:

var mixedArray = personArray.concat(petArray).sort(
  function (a, b) {
    return a.age - b.age;
  }
);

Results in:

[
  {
    "name": "Fluffy",
    "age": 4
  },
  {
    "name": "Spaz",
    "age": 5
  },
  {
    "name": "Amanda",
    "age": 25
  },
  {
    "name": "Jack",
    "age": 29
  }
] 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Combine multiple arrays and add there values

Get objects containing max values for multiple fields using aggregation in mongodb

Getting specific object property values from multiple arrays

How to merge values from multiple objects into an object of arrays?

Iterate through 2 arrays containing objects and return the values of a second array using the first arrays keys

Filtering JavaScript objects using values from multiple arrays

How to sort an array of objects in the order of another array containing the unique values of a property present in each object?

Reduce an object containing arrays as values to single array

Combine multiple arrays with identica keys and different values

Combine two arrays with multiple values and same keys

Combine multiple observable arrays into new object array

How to combine multiple arrays into single JSON object

How to combine multiple arrays in one JSON object

Combine Multiple Arrays + Variable into single JSON object

Merge two arrays into an array of objects with property values

Find duplicate objects based on a property and combine the values of the property

Filter an array of objects using multiple values from the object

Swift: Most efficient way to split array of objects into multiple arrays based on object property?

How can I retrieve dynamically specified, arbitrary and deeply nested values from a Javascript object containing strings, objects, and arrays?

How to assign values from object by checking property name to array of objects via property name using Angular

How to combine multiple DataFrame rows into 1 with a column containing list values

How to read a single object from a json file containing multiple objects using python?

How to convert an object with arrays as values to an array of objects

Using Jest property matchers on arrays of objects

Transforming array of objects containing arrays into a unique object lookup with lodash

How to fetch specific object from json containing arrays of objects

Iterating over an array of class objects VS a class object containing arrays

Converting nested arrays in JavaScript into flat objects containing members of the parent object

Compare objects containing arrays