简体   繁体   中英

how to make array of object using an array?

I have one array

var ar=['aa','cc','po']

I want to push objects in new array after checking the value of given array .In other words

I have these given conditions

var ar=['aa','cc','po']

var arr =[{name:"po"},{name:'aa'},{name:'cc'}];

Expected Output in new Array

  [{name:'aa'},{name:'cc'},{name:"po"}]

As "aa" in in 0 index then I added object whose name property aa .

I try like this .but I used two for look .is there any simple way to do this

FIDDLE

var newArr=[];
for(var i=0;i<ar.length ;i++){
   var text =ar[i];
   for(var j=0;j<arr.length ;j++){
    var obj =arr[j];
    console.log(obj.name);
     /*if(obj.name===text){
       newArr.push(obj);
     }*/
    }
}
console.log(newArr);  

This is a proposal in two part, first build an object with the reference to the items of arr and the create a new array with the given items of ar .

 var ar = ['aa', 'cc', 'po'], arr = [{ name: "po" }, { name: 'aa' }, { name: 'cc' }], object = Object.create(null), result = []; arr.forEach(function (a) { object[a.name] = a; }); ar.forEach(function (a) { object[a] && result.push(object[a]); }); document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>'); 

Using forEach iterator and generate object reference based on name and then generate result array using map()

 var ar = ['aa', 'cc', 'po'] var arr = [{ name: "po" }, { name: 'aa' }, { name: 'cc' }]; var ref = {}; // generating object reference with name property arr.forEach(function(v) { ref[v.name] = v; }); // generating result array // or you can use forEach as @NinaScholz answer var res = ar.map(function(v) { return ref[v]; }).filter(function(v) { // use filter to avoid null values , in case of missing elements return v != null; }); document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>'); 

Try this:

function convert(source) {

  var 
    obj = [],
    i;

  for (i = 0; i < source.length; ++i) {

    obj.push({name: source[i]});
  }

  return obj;
}

convert(['aa', 'bb', 'cc']); // [{name:'aa'},{name:'bb'},{name:'cc'}]

This would work if you want to assign values from array in sequence:

var ar=['aa','cc','po']

var arr =[{name:"po"},{name:'aa'},{name:'cc'}];

arr.map(function(obj,key){
    obj.name = ar[key];
});

console.log(arr);

Do like this

var ar = ['aa', 'cc', 'po']
var arr = [{  name: "po"}, {  name: 'aa'}, {  name: 'cc'}];
$.each(ar, function(i, v) {
  arr[i].name = v;
});
console.log(arr)

Fiddle

var array=['a','b','c'];
var arrayObj=[];
for(var i=0;i<array.length;i++){
 var obj={};
 obj.name=array[i];
 arrayObj.push(obj);
}
console.log(JSON.stringify(arrayObj));

Output: [{"name":"a"},{"name":"b"},{"name":"c"}]

I guess this is one very functional way of doing this job with no more than an assignment line. However Anoop Joshi's answer is more elegant provided that the ar array is shorter than equal to in length to the arr array.

 var arr = ['aa','cc','po'], ar = [{name:"po"},{name:'aa'},{name:'cc'}], res = arr.map(e => ar[ar.findIndex(f => f.name == e)]); document.write("<pre>" + JSON.stringify(res) + "</pre>"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM