简体   繁体   English

将对象数组转换为字符串数组?

[英]Convert an array of object to array of string?

I'm having an array of object like this- 我有这样的对象数组-

var person = [
          {name: 'saprsh', age: 22, address:'XYZ'},
          {name: 'Ankur', age: 23},
          {name: 'Richa', age:25, adddress:'ABX', email:'abc@xyz.co'} 
];

now i want output like this 现在我想要这样的输出

var string_person = [{sparsh22XYZ},{ankur23},{Richa25ABXabc@xyz.co}];

is their any way to get output like this in javascript, jquery, Angular.js. 是他们在javascript,jquery,Angular.js中获取此类输出的任何方式。

Any other web used language is approved. 批准使用任何其他网络使用的语言。

Check out this jsfiddle . 看看这个jsfiddle You'll see both Array.prototype.reduce and Array.prototype.map used, both with the same results. 您将看到同时使用了Array.prototype.reduceArray.prototype.map ,两者的结果相同。

This is classic reduce : 这是经典的reduce

var people = person.reduce(function(agg, p) {
  return agg.concat([p.name + p.age + p.address]);
}, []);

The above uses Array.prototype.reduce . 上面使用Array.prototype.reduce

In other words, when you want all the properties of an object or array "reduced" into something, then the most semantic go-to option is probably Array.prototype.reduce in this case. 换句话说,当您希望将对象或数组的所有属性“简化”为某种东西时,在这种情况下,最语义化的选择可能就是Array.prototype.reduce

However, Array.prototype.map can also do the job quite cleanly: 但是, Array.prototype.map也可以很干净地完成此工作:

var people = person.map(function(p) {
  return p.name + p.age + p.address;
});

This is an argument, now, between readability/complexity vs. semantics. 现在,这是可读性/复杂性与语义之间的一个争论。

To limit incidental complexity (in the form of readability), I might go for the map function, even though you could argue this is technically a paradigmatic reduction. 为了限制附带的复杂性(以可读性的形式),我可能会使用map函数,即使您可以从技术上讲这是范式简化。

输出

I think it will help you. 我认为它将为您提供帮助。

var person = [
          {name: 'saprsh', age: 22, address:'XYZ'},
          {name: 'Ankur', age: 23, address:'ABC'}
];  
var stringarray=[];  

//  $.each(person, function (i, d) {
//      stringarray.push(d.name + d.age + d.address);
//  });

//for(var i = 0; i < person.length; i++){
//    stringarray.push(person[i].name + person[i].age + person[i].address);
//}

var stringarray = person.map(function(p) {
    return p.name + p.age + p.address;
});

console.log(stringarray);

Result: ["saprsh22XYZ", "Ankur23ABC"] 结果:[“ saprsh22XYZ”,“ Ankur23ABC”]

Plz Try this one. 请尝试这个。

Try this, this method suitable for different object names, it will work good. 试试这个,此方法适合于不同的对象名称,它将很好地工作。

var person = [
          {name: 'saprsh', age: 22, address:'XYZ'},
          {name: 'Ankur', age: 23},
          {name: 'Richa', age:25, adddress:'ABX', email:'abc@xyz.co'} 
];
var result = person.map(function(p){ return Object.keys(p).map(function(k){return p[k]}).join("");})

I assume you want a array of strings. 我假设您想要一个字符串数组。

[{sparsh22XYZ},{ankur23ABC}] 

is not such an array. 不是这样的数组。

If you want 如果你想

[ "sparsh22XYZ", "ankur23ABC" ]

you can simply go with 你可以简单地去

Plain old Javascript: 普通的旧Javascript:

var string_person = [];
for (var i = 0; i < person.length; i++) {
    string_person.push(person[i].name+person[i].age+person[i].address);
}

Underscore.js library Underscore.js库

If all you need is a list of values of one of the object properties, it's easiest to go with underscore.js library. 如果你需要的是对象属性的一个值的列表,最简单的方法去与underscore.js库。

var string_person = _.pluck(person, 'name');

http://underscorejs.org/#pluck http://underscorejs.org/#pluck

You can do it like this. 您可以这样做。

var person = [
      {name: 'saprsh', age: 22, address:'XYZ'},
      {name: 'Ankur', age: 23, address:'ABC'}
];

var test = person.map(function(one){
  var properties = Object.getOwnPropertyNames(one);
  return properties.map(function(prop){
    return one[prop];
  }).join('');
});
console.log(test);

Call the below function on any array of Objects with any number of parameters, it will return you what you want. 在具有任意数量参数的任何对象数组上调用以下函数,它将返回您想要的内容。

function getStringArray(array){
                var resultArray = [];
                for (i = 0; i < array.length; i++) { 
                    var result = "";
                    var keysArray = Object.keys(array[i]).sort()
                    for(j = 0; j < keysArray.length; j++){
                        result = result+array[i][keysArray[j]];
                    }
                    resultArray.push(result);
                }
                return resultArray;
            }
var string_person = [];
for(var i = 0; i < person.length; i++){
    string_person.push(person[i].name + person[i].age + person[i].address);
}

Updated: 更新:

Also You can use Underscore: 您也可以使用下划线:

var string_person = _.map(person, function(p){return p.name + p.age + p.address;});

I guess you want to join all members of the object to a string. 我猜您想将对象的所有成员连接到字符串。 There are two ways to do this: 有两种方法可以做到这一点:

// iterate through the array of persons
for (var index = 0; index < person.length; index++) {
    var obj = person[index]; // save the object temporally
    person[index] = ''; // place an empty string at the index of the object
    // iterate through all members of the object using the "in"-operator
    for (var member in obj) {
        person[index] += obj[member]; // add the value of the member to the string
    }
}

The problem with this technique is, I cannot guarantee that it will join the values of the members in the order you want. 这种技术的问题是,我不能保证它会按您想要的顺序加入成员的值。 It should join them in the order in which the members were defined. 它应该按照成员定义的顺序加入他们。

Anyway this solution works fine but only in your case: 无论如何,此解决方案可以正常工作,但仅适用于您的情况:

// iterate through the array of persons
for (var index = 0; index < person.length; index++) {
    // place a string which contains the joined values of the members in the right order at the index of the object
    person[index] = [
        person[index].name,
        person[index].age,
        person[index].address
    ].join('');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM