Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中数组中的重复值添加_Javascript - Fatal编程技术网

Javascript中数组中的重复值添加

Javascript中数组中的重复值添加,javascript,Javascript,我有一个这样的物体: totalProduct = [{name: "A", string: "Hello"}, {name: "A", string: "Hello"}, {name: "B", string: "Hey"}]; 我计算了相同名字的总数。到目前为止一切都很好,我做到了: let counts = {}; for (const i in totalProduc

我有一个这样的物体:

totalProduct = [{name: "A", string: "Hello"}, {name: "A", string: "Hello"}, {name: "B", string: "Hey"}];
我计算了相同名字的总数。到目前为止一切都很好,我做到了:

let counts = {};
for (const i in totalProduct) {
  const nameObject = totalProduct[i].name;
  const stringObject = totalProduct[i].string;

  counts[nameObject] = (counts[nameObject]||0) + 1;
}
输出如下:

{A:2, B:1}
但是,我希望有这样的输出(将对象值放入带有字符串的数组中),但我不知道如何做到这一点:(

我试过这么做,但不起作用:

let counts = {};
for (const i in totalProduct) {
  const nameObject = totalProduct[i].name;
  const stringObject = totalProduct[i].string;

  counts[nameObject] = [(counts[nameObject]||0) + 1, stringObject];
}

迭代数组时,不要将计数作为对象中的值,而是将
[number,string]
数组(与所需对象类似)放入对象中。在每次迭代中,如果属性尚不存在,则在对象内创建数组,然后增加数组中的
[0]
第项,无论:

const arr=[{name:“A”,string:'Hello'},{name:“A”,string:'Hello'},{name:“B”,string:'Hey'}];
常量obj={};
for(arr的常量{name,string}){
如果(!obj[name])obj[name]=[0,string];
obj[name][0]++;
}

console.log(obj);
迭代数组时,不要将计数作为值放在对象中,而是将
[number,string]
数组(如所需对象)放在对象上。在每次迭代中,如果属性尚不存在,则在对象内创建数组,然后增加
[0]
th数组中的项目,不管:

const arr=[{name:“A”,string:'Hello'},{name:“A”,string:'Hello'},{name:“B”,string:'Hey'}];
常量obj={};
for(arr的常量{name,string}){
如果(!obj[name])obj[name]=[0,string];
obj[name][0]++;
}

console.log(obj);
您需要将计数[nameObject]设置为两个元素的数组

consttotalproduct=[{name:“A”,string:“Hello”},{name:“A”,string:“Hello”},{name:“B”,string:“Hey”}];
让计数={};
用于(totalProduct中的常数i){
const nameObject=totalProduct[i]。名称;
const stringObject=totalProduct[i]。字符串;
const prevObj=计数[名称对象]
计数[nameObject]=[(prevObj?prevObj[0]:0)+1,stringObject];
}

console.log(counts);
您需要将
counts[nameObject]
设置为两个元素的数组

consttotalproduct=[{name:“A”,string:“Hello”},{name:“A”,string:“Hello”},{name:“B”,string:“Hey”}];
让计数={};
用于(totalProduct中的常数i){
const nameObject=totalProduct[i]。名称;
const stringObject=totalProduct[i]。字符串;
const prevObj=计数[名称对象]
计数[nameObject]=[(prevObj?prevObj[0]:0)+1,stringObject];
}

console.log(counts);
这些字符串对象来自哪里?也就是说,
Hello
Hey
,它们来自哪里?Hello和Hey来自
const-stringObject=totalProduct[i].string;
totalProduct=[{name:“A”},{name:“A”},{name:“B”}]
这没有一个属性
string
对于其
name
属性上具有值
a
的每个对象,它们也将具有相同的
string
值?是否存在
{name:'a',string:'Hello'}
{name:'a',string:'Hey'}
?这些字符串对象来自哪里?也就是说,
Hello
Hey
,它们来自哪里?Hello和Hey来自
const-stringObject=totalProduct[i].string;
totalProduct=[{name:“A”},{name:“A”},{name:“B”}]
这没有属性
字符串
对于其
名称
属性上具有值
a
的每个对象,它们也将具有相同的
字符串
值?是否存在
{name:'a',string:'Hello'}
{name:'a',string:'Hey'}
let counts = {};
for (const i in totalProduct) {
  const nameObject = totalProduct[i].name;
  const stringObject = totalProduct[i].string;

  counts[nameObject] = [(counts[nameObject]||0) + 1, stringObject];
}