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.

3
  • you mean to remove the hashes and just put the values in? Commented Aug 26, 2011 at 21:54
  • I need to clean away everything from the array except the numbers so that the finished products is as example 2. Commented Aug 26, 2011 at 21:57
  • The word "cleaning" indicates the wrong mindset. You are not "cleaning" a datastructure. You are extracting information from one datastructure to build another datastructure. Commented Aug 26, 2011 at 22:12

11 Answers 11

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

1 Comment

+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.
0

Do something like:

var cleanedArray = [];
for(var i=0; i<data.length; i++) {
  cleanedArray.push(data[i].id);
}
data = cleanedArray;

Comments

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)

1 Comment

Avoid iterating Array with for...in.
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.

Comments

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

Comments

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

Comments

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

Comments

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

Comments

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']

Comments

Your Answer

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.