简体   繁体   English

如何将具有相同属性名称的对象数组转换为从给定数组中的值连接值的对象? JS

[英]How to transform an array of objects with the same property name into the object in which value is concatenated from values in a given array? JS

I have an array of objects which looks like this:我有一个看起来像这样的对象数组:

const arr = [
  {integer: "3"},
  {integer: "500"}
]

As a result i want to get the object that looks like this: mergedObj = {digits: "3500"}结果我想得到看起来像这样的对象: mergedObj = {digits: "3500"}

I tried to use reduce, but it seems that i'm doing something wrong.我尝试使用reduce,但似乎我做错了什么。 Here is my code:这是我的代码:

var mergedObj = arr.reduce((acc, obj) => {

   acc["digits"] = [acc[obj.integer]].concat(obj.integer).join("");

    return acc;
}, {});

Here is the result of what i'm getting:这是我得到的结果:

"Object is", [object Object] {
  digits: "500"
}

The problem is that i'm missing the first part of my digit.问题是我缺少数字的第一部分。 Please help me!请帮我! Thank you in advance!先感谢您!

Use Array.map() to create an array of integer , and then join.使用Array.map()创建一个integer数组,然后加入。 Assign to digits property of a new object:分配给新对象的digits属性:

 const arr = [ {integer: "3"}, {integer: "500"} ] const result = { digits: arr.map(o => o.integer).join('') }; console.log(result);

You almost got this.你几乎得到了这个。 The [acc[obj.integer]] part should be [acc.digits] . [acc[obj.integer]]部分应该是[acc.digits] Anyway, you can do it much simpler:无论如何,你可以做得更简单:

 const arr = [ {integer: "3"}, {integer: "500"} ] var mergedObj = arr.reduce((acc, obj) => { acc.digits += obj.integer; return acc; }, { digits: '' }); console.log(mergedObj);

Try this below :试试这个:

const arr = [
   {integer: "3"},
   {integer: "500"}
]

let output = {'digits':''};
arr.map(function (item) {
    output['digits'] += item['integer']
});
console.log(output);

暂无
暂无

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

相关问题 在对象数组中的属性中查找具有最大值的所有对象,并从同一对象返回其他属性的值 - Finding all objects with Max value in a property within an Array of Objects and return values of other property from the same object 如何通过使用 Angular 通过属性名称将属性名称检查到对象数组来从 object 分配值 - How to assign values from object by checking property name to array of objects via property name using Angular 如何将对象键分配给相同的属性并创建具有名称和值对的对象数组 - how to assign Object keys to a same property and create an array of objects with name and value pair-Javascript AngularJS:如何使用$ filter在给定的属性值数组中从对象数组中查找对象 - AngularJS: how to find objects from an array of objects, given an array of property values in an array, using $filter 从对象数组中提取具有另一个属性等于给定值的项目的属性值数组 - From array of objects extract array of property values for items having another property equal to given value 如何将对象的 object 转换为对象数组的 object js - how to transform object of objects to object of array of objects js 如何在 Object 中搜索包含作为数组值的子对象的值 - How to search for a value in Object which contains sub objects with as array values 使用相同的键(将值作为数组)将多个对象转换为一个对象 - Transform multiple objects into one object with same key, value as an array 将具有相同属性的对象数组转换为具有数组值的一个对象 - Convert array of objects with same property to one object with array values 比较数组中的对象,合并具有相同属性值的重复项,将属性值添加到合并的对象中 - Compare objects in array, merge duplicates with same property value, add property values to the merged object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM