how to create an array of objects based on another array of objects?

filter an array of objects from another array of objects javascript
filter array of objects from another array of objects
javascript update object in array es6
javascript array of objects
filter array of objects with array of objects javascript
javascript map array of objects
javascript update object property in array
javascript object with array property

I have this array:

var arr1 = [
 {"user":"dan","liked":"yes","age":"22"},
 {"user":"sarah","liked":"no","age":"21"},
 {"user":"john","liked":"yes","age":"23"},
];

I'd like to create a new (sub)array of that array, containing only the likes of the users.

so it would look like this:

var arr2 = [
 {"dan":"yes"},
 {"sarah":"no"},
 {"john":"yes"},
];

I tried:

var arr2 =[];
for(var i in arr1){
  arr2.push({[i[user]]:i[liked]});
}

it needs a tweak, ideas?


Use array.map

var arr1 = [
 {"user":"dan","liked":"yes","age":"22"},
 {"user":"sarah","liked":"no","age":"21"},
 {"user":"john","liked":"yes","age":"23"},
];

var arr2 = arr1.map(v => ({ user: v.user, liked: v.liked }));

console.log(arr2);

Useful Javascript Array and Object Methods, Array. from() This is a static method that creates an array based on another array or string. You can also pass a map callback function as an argument to further shape the data in the new array. Order an array of objects based on another array order - mapOrder.js


Based on your edit:

arr1 = arr1.map(function(item){
  return{
    [item.user]: item.liked
  }
});

How to filtering array of objects to another array of objects in , Then you need to make your logic other way around, filter data based on permission, right? 1 Like. shahporancse March 24, 2020, 11� Since you haven't specified anything for population in your properties definition, it is initialised as an empty array of double. So when you try to grow the population array in the constructor matlab is not happy since you're trying to assign a RobotCreature to an array of double. The easiest fix: create population as an empty array of


var arr1 = [
 {"user":"dan","liked":"yes","age":"22"},
 {"user":"sarah","liked":"no","age":"21"},
 {"user":"john","liked":"yes","age":"23"},
];

var res = arr1.map(function(o) {
  var r = {};
  r[o.user] = o.liked;
  return r;
});

console.log(res);

Array methods, The arrays are objects, so we can try to use delete : concat. The method arr. concat creates a new array that includes values from other arrays and additional items. The function is called for elements of the array, one after another: They are used to calculate a single value based on the array. Below is a list of useful methods which will help you to work with JavaScript arrays and objects easier! The filter() method creates an array filled with all array elements that meet a certain…


Try this

var arr2 =[];
for(var i in arr1){
    var obj= {};
    obj[arr1[i]['user']] = arr1[i]['liked'];
    arr2.push(obj)
} 

Order an array of objects based on another array order � GitHub, Sort array of objects based on another array. */. function mapOrder (array, order, key) { array; array.forEach(function(it) { map.set(it[key], index++); }); order. Another option would be: simple_object *temp = new simple_object[20]; for (i to 10) { temp[i] = array[i]; } delete [] array; array = temp; That way every element in the array is default constructed and the copy constructor for each object is called directly. Is this the proper way to copy arrays of objects or are there other more standard ways


var arr2=[];
arr1.forEach(function(obj)
{
    var temp=new Object();
    temp[obj.user]=obj.liked;
    arr2.push(temp);
});

I think this is what you want

JavaScript Array Filter: Filtering Array Elements Based on a Test , One of the most common tasks when working with an array is to create a new array Suppose you have an array of city objects where each object contains two� JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).


Filter array with another array, This is little neater IMO. // User can be part of many groups const user = { groups: ["group2"] }; // Roles can have many groups // What you see� a. Write a FractionDemo program that instantiates several Fraction objects and demonstrates that their methods work correctly. Create a Fraction class with fields that hold a whole number, a numerator, and a denominator. In addition: • Create properties for each field.


Array.from(), The Array.from() static method creates a new, shallow-copied Array arrayLike: An array-like or iterable object to convert to an array. In ES2015, the class syntax allows sub-classing of both built-in and user-defined classes. Powershell Creating Array of Objects based on one column End in a Return statement so you can pipe this command to another cmdlet instead of having it just write


Quick Tip: How to Sort an Array of Objects in JavaScript — SitePoint, Let's say that foo and bar are the two elements being compared by the compare function, and the return value of the compare function is set up as� In this article, I’ll discuss arrays and objects. There are two major types of arrays: indexed array and associative array. An “indexed” array is one where the index must be an integer, and