0

So I'm getting values and I want to put them into an array:

    var values = {25,23,21}
    var unit = {PCS,KG,PCS}

I want it to be like this when I put them in an array

    array = {25:PCS,23:KG,21:PCS}

and group and add them depending on the unit so the final result will be something like this

    totalval = {46:PCS,23:KG}

I can only put those values into separate arrays but I don't know how can I combine and group them..

3
  • 4
    these are objects, not arrays. Commented Oct 22, 2015 at 4:06
  • oh sorry, as I'm new when it comes to this kind of problem.. Commented Oct 22, 2015 at 4:07
  • looking at this question it appears you actually want arrays, that form into one object. You also can't have numbers as property names. Commented Oct 22, 2015 at 4:09

3 Answers 3

2

https://jsfiddle.net/wyh5a2h2/

I went through reorganizing your code so it makes a bit of sense and came up with this: Hopefully it suits what you are attempting to do:

var items = [
    {value: 25, unit: 'PCS'},
    {value: 23, unit: 'KG'},
    {value: 21, unit: 'PCS'},
]

var numPCS = 0, numKG = 0;
var result = [];

items.forEach(function(elem, index) {
    if(elem.unit==='PCS') {
        numPCS+=elem.value;
    }
    if(elem.unit==='KG') {
        numKG+=elem.value;
    }
});

result.push({value: numPCS, unit: 'PCS'});
result.push({value: numKG, unit: 'KG'});

console.log(result);

Here is the result:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

That's pretty much it. I simplified your fiddle a bit and made it generic here
It works great! But what if my unit value is dynamic? How can I still group them??
@James Yeah I wasn't really sure what the OP wanted. It was a mixture of arrays and objects that didn't make sense, so I just did what I thought looked similar...
@user2119308 If the unit is dynamic, put the units in an array, and then iterate through them, while comparing them...
1
var values = [25, 23, 21],
    unit = ['PCS', 'KG', 'PCS'],
    result = {},
    tmpval = {},
    totalval = {};

for (var i = 0; i < values.length; i++) {
    result[values[i]] = unit[i];
    tmpval[unit[i]] || (tmpval[unit[i]] = 0);
    tmpval[unit[i]] += values[i];
}

for (var i in tmpval) {
    totalval[tmpval[i]] = i;
}

// result: { '21': 'PCS', '23': 'KG', '25': 'PCS' }
// totalval: { '23': 'KG', '46': 'PCS' }

Comments

1

you will have to create objects first and then put them into an array

 var obj1 =  {
     value: 25,
     unit: "PCS"   
 };

 ...

 var array = [ obj1, obj2, obj3, ... ];

then aggregate them accordingly

4 Comments

Did I just see you name an object array ?!?!?!
@MaxMastalerz array is also object, it's not primitive data type. Nevertheless the example is wrong: it should either use the object keys or the array notation.
I know an array is an object, but it's not useful to call it that. Just call it array. plzzzzz
Devil's advocate: An object in javascript is technically an associative array. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.