0

I need to group item having same name property and increase their 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.

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
Thi Tran
  • 681
  • 5
  • 11
  • http://stackoverflow.com/questions/31601807/add-item-to-array-by-key-in-javascript/31602203#31602203 please see this I used array to store the result – Bibek Sharma Jul 24 '15 at 04:30

4 Answers4

0

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>
Bibek Sharma
  • 3,210
  • 6
  • 42
  • 64
-1

E/ My previous answer was incorrect.

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.

{} is an object. Objects can have string indexes.

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

Why can't result be {}?

Arhowk
  • 901
  • 4
  • 11
  • 22
  • it seems not to work in this case, please explain to me the reason why it doesn't work – Thi Tran Jul 24 '15 at 03:47
  • But you just said it worked. I explained the reasons why {} works and [] doesn't. – Arhowk Jul 24 '15 at 03:47
  • actually `arr['foo']` will create a property named `foo` to the object `arr`, even if its type is Array. But then it won't be part of the Array prototype and thus not enumerable by index – Kaiido Jul 24 '15 at 03:48
  • If you'd like it to maintain the same numbering schema as the original Object, than you can do result[i] = orgArr[i] – Arhowk Jul 24 '15 at 03:49
  • No, i mention your solution before you've edited. I dont know it works when using an object instead of an array – Thi Tran Jul 24 '15 at 03:49
  • I had this issue when I tried to solve another here: http://stackoverflow.com/questions/31601314/how-to-get-total-count-of-specific-key-from-json-using-jquery/31601410#31601410 – Thi Tran Jul 24 '15 at 03:52
-1

if you want it to work, make result = {} not []

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. so you get nothing in your output.

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
-1

You get empty array it's because by default JSON.stringify failed to convert your array to json string.

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.

Can you explain why would you want to convert json array to an array with string index. It's always better to use json array if you want a string as key/index.

For more information about JSON.stringify

Peter Wong
  • 430
  • 5
  • 12