0

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); 
Pranav C Balan
115k25 gold badges173 silver badges195 bronze badges
asked May 3, 2016 at 7:18
1
  • ar and arr? That's not confusing at all... Commented May 3, 2016 at 7:49

7 Answers 7

4

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>');

answered May 3, 2016 at 7:26
Sign up to request clarification or add additional context in comments.

Comments

3

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>');

answered May 3, 2016 at 7:22

2 Comments

@parnav this array may be this ['po' ,'cc','aa'] not sure it is not formatted way
is there any way to do in lodash
1

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'}]
answered May 3, 2016 at 7:23

Comments

1

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);
answered May 3, 2016 at 7:31

Comments

0

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

answered May 3, 2016 at 7:22

Comments

0
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"}]

answered May 3, 2016 at 7:37

Comments

0

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>");

answered May 3, 2016 at 16:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.