简体   繁体   中英

How to "clean" an array in javascript?

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.

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

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

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.

The simplest way to clean any ARRAY in javascript its apply a empty data after the your ARRAY name, like this:

myArray = [1,2,3,4,5,'etc']
myArray = []
console.log(myArray)
[]

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

Do something like:

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

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)

使用Array.prototype.map此文档页面中定义了回退代码,如果您的用户的浏览器缺少它,它将定义该功能。

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

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

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

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