1

I am stuck here. How can I clean this array:

{"data":[{"id":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]}

So that it looks like:

["5201521d42","52049e2591","52951699w4"]

I am using Javascript.

Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175

11 Answers11

4

You just need to iterate over the existing data array and pull out each id value and put it into a new "clean" array like this:

var raw = {"data":[{"":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]};
var clean = [];
for (var i = 0, len = raw.data.length; i < len; i++) {
    clean.push(raw.data[i].id);
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

If you can use ES5 and performance is not critical, i would recommend this:

Edit: Looking at this jsperf testcase, map vs manual for is about 7-10 times slower, which actually isn't that much considering that this is already in the area of millions of operations per second. So under the paradigma of avoiding prematurely optimizations, this is a lot cleaner and the way forward.

var dump = {"data":[{"id":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]};
var ids = dump.data.map(function (v) { return v.id; });

Otherwise:

var data = dump.data;
var ids = [];
for (var i = 0; i < data.length; i++) {
  ids.push(data[i].id);
}
evilpie
  • 2,718
  • 20
  • 21
  • +1 I was *just* about to post the `.map()` answer. I'll bet it gets pretty darn good performance as a native method, but not quite as much as a shim. – user113716 Aug 26 '11 at 22:02
1

Overwriting the same object

var o = {"data":[{"id":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]};

for (var i = o.data.length; i--; ){
   o.data[i] = o.data[i].id;
}

What you're doing is replacing the existing object with the value of its id property.

Community
  • 1
  • 1
vol7ron
  • 40,809
  • 21
  • 119
  • 172
0

The simplest way to clean any ARRAY in javascript its using a loop for over the data or manually, like this:

  let data = {"data":[{"id":"5201521d42"},{"id":"52049e2591"}, 
    {"id":"52951699w4"}]};
  let n = [data.data[0].id,data.data[1].id, data.data[2].id];
console.log(n)

output:

(3) ["5201521d42", "52049e2591", "52951699w4"]
0

Easy and a clean way to do this.

oldArr = {"data":[{"id":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]}
oldArr  = oldArr["data"].map(element => element.id)

Output: ['5201521d42', '52049e2591', '52951699w4']

0

Do something like:

var cleanedArray = [];
for(var i=0; i<data.length; i++) {
  cleanedArray.push(data[i].id);
}
data = cleanedArray;
JoshNaro
  • 2,047
  • 2
  • 20
  • 40
0

Take a look at this fiddle. I think this is what you're looking for

oldObj={"data":[{"":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]};
oldObj = oldObj.data;
myArray = [];

for (var key in oldObj) {

   var obj = oldObj[key];
   for (var prop in obj) {
      myArray.push(obj[prop]);
   }
}
console.log(myArray)
Amin Eshaq
  • 4,034
  • 1
  • 17
  • 21
  • 1
    [Avoid](http://stackoverflow.com/questions/500504/javascript-for-in-with-arrays) iterating `Array` with `for`...`in`. – bobince Aug 26 '11 at 22:14
0

Use Array.prototype.map there is fallback code defined in this documentation page that will define the function if your user's browser is missing it.

erikvold
  • 15,988
  • 11
  • 54
  • 98
0
var data = {"data":[{"":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]}; 
var clean_array = []; 
for( var i in data.data )
{ 
    for( var j in data.data[i] ) 
    { 
        clean_array.push( data.data[i][j] ) 
    } 
} 
console.log( clean_array );
aziz punjani
  • 25,586
  • 9
  • 47
  • 56
0

You are actually reducing dimension. or you may say you are extracting a single dimension from the qube. you may even say selecting a column from an array of objects. But the term clean doesn't match with your problem.

var list = [];
var raw = {"data":[{"id":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]};
for(var i=0; i < raw.data.length ; ++i){
  list.push(raw.data[i].id);
}
Neel Basu
  • 12,638
  • 12
  • 82
  • 146
0

Use the map function on your Array:

data.map(function(item) { return item.id; });

This will return:

["5201521d42", "52049e2591", "52951699w4"]

What is map? It's a method that creates a new array using the results of the provided function. Read all about it: map - MDN Docs

Peter
  • 3,998
  • 24
  • 33