简体   繁体   English

在JavaScript中按键将项目添加到数组

[英]Add item to array by key in JavaScript

I need to group item having same name property and increase their number . 我需要对具有相同name属性的项目进行分组,并增加其number I try to assign to new array and check existed item by key. 我尝试分配给新数组,并按键检查已存在的项目。 It works fine when I use this solution in PHP, but in JavaScript it doesn't. 当我在PHP中使用此解决方案时,它可以正常工作,但在JavaScript中却不能。

I have searched some similar questions, but I don't know why it doesn't work. 我搜索了一些类似的问题,但是我不知道为什么它不起作用。

 var orgArr = [ {name: 'abc', number: 3}, {name: 'xyz', number: 2}, {name: 'abc', number: 5} ]; var result = []; //work if result = {}; for (var i = 0; i < orgArr.length; i++) { if (!result[orgArr[i].name]) { result[orgArr[i].name] = orgArr[i]; //assign new } else { result[orgArr[i].name].number += orgArr[i].number; //increase number if name exist in result array } } alert(JSON.stringify(result)); //expect array 2 item but it's empty array console.log(result); //Will have result 2 item when I view console window 

 var orgArr = [ {name: 'abc', number: 3}, {name: 'xyz', number: 2}, {name: 'abc', number: 5} ]; var result = []; //work if result = {}; var tempArray = []; // used to store unique name to prevent complex loop orgArr.forEach(function(item){ if($.inArray(item.name, tempArray)< 0){// unique name result.push(item); tempArray.push(item.name); } else{ var indexNew = $.inArray(item.name, tempArray); result[indexNew].number += item.number; } }); alert(JSON.stringify(result)); //expect array 2 item but it's empty array console.log(result); //Will have result 2 item when I view console window 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

E/ My previous answer was incorrect. E /我以前的回答不正确。

Well.. yes... 嗯,是...

[] designates an array. []指定一个数组。 An array holds everything by integer values. 数组按整数值保存所有内容。 You're trying to feed it a string value (orgArr[x].name) and since its just an array, it discards it as a bad call. 您正在尝试为它提供一个字符串值(orgArr [x] .name),并且由于它只是一个数组,因此将其丢弃为错误调用。

{} is an object. {}是一个对象。 Objects can have string indexes. 对象可以具有字符串索引。

var result = []; //work if result = {}; 

Why can't result be {}? 为什么结果不能为{}?

if you want it to work, make result = {} not [] 如果您希望它起作用,请使result = {}而不是[]

an array is not a key value store. 数组不是键值存储。 It still will take key values, and will store them on the array object. 它仍将采用键值,并将其存储在数组对象上。 But when stringify encounters your array, it determines it is an array and then trys to iterate the array, and your array appears empty. 但是当stringify遇到您的数组时,它确定它是一个数组,然后尝试迭代该数组,并且您的数组显示为空。 so you get nothing in your output. 这样您的输出中什么也不会得到。

You get empty array it's because by default JSON.stringify failed to convert your array to json string. 您得到空数组是因为默认情况下JSON.stringify无法将您的数组转换为json字符串。

By default JSON.stringify converts int-based index array to json string but you are using orgArr[i].name which is a string as array index that the cause alert() show empty array. 默认情况下,JSON.stringify将基于int的索引数组转换为json字符串,但是您使用的是orgArr [i] .name,它是一个字符串,作为数组索引,导致alert()显示空数组。

Can you explain why would you want to convert json array to an array with string index. 您能解释一下为什么要将json数组转换为带有字符串索引的数组吗? It's always better to use json array if you want a string as key/index. 如果要将字符串作为键/索引,最好使用json数组。

For more information about JSON.stringify 有关JSON.stringify的更多信息

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

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