I'm trying to map values to a new array. The problem is that the property that I'm mapping can either be a number or an array.
I have a problem when it comes to arrays, because my result is an associative array. My goal is to turn [[1,2,3], [1,2,3]] (see below) in to [1,2,3,1,2,3]. In other words; just make it a one dimensional array.
I've tried with a foreach loop inside the map(), without success. Any ideas?
var list = [{ foo: 1, bar: [1,2,3] }, { foo: 2, bar: [1,2,3] }];
var retArr = list.map(function (v) {
return v.bar;
});
console.log(retArr); // [[1,2,3], [1,2,3]]
var list = [{ foo: 1, bar: [1,2,3] }, { foo: 2, bar: [1,2,3] }];
var retArr = list.map(function (v) {
if($.isArray(v.bar)){
$.each(v.bar, function(i, v){
return v;
});
} else {
return v.bar;
}
});
console.log(retArr); // [undefined, undefined]