0

I run trough a couple of div's with the jQuery .each function.

With console.log I get this output

0.24 240,

0.1 100, 

0.24 240,

0.24 240,

The first number on each line is a factor and the last is the sum of a multiplication

I would like to make a hash like this when the each function runs

a = {0.24: 720, 0.1: 100}

The number 0.24 as key, then sum up the the second number on each 0.24 line as value and so on.

Ideas?

Update and my solution

jQuery.fn.myFunction = function(){
 var hash = {}
  this.each(function(index, element) {
    var key = var key = $(element).attr("data-key");
    var value = $(element).attr("data-value");

    if (hash.hasOwnProperty(key)){
      hash[key] = hash[key] + value
    }else{
      hash[key] = value;
    }

    });
  console.log(hash)
}
2
  • 3
    you shoulkd post code relevant to the output you get, the each loop Commented Dec 13, 2013 at 11:55
  • I think that, in this case it would make it unclear. It's an easy consept. Loop and make hash. Commented Dec 13, 2013 at 13:01

3 Answers 3

1

I assume you have key and value, though it will be better if you provide at least javascript you use.

var jsonObject = {}
function makeObject(key,value)
{
 jsonObject[key] = value + parseInt(jsonObject[key] == undefined ? 0 : jsonObject[key]);
 jsonObject[key] = value;
}

call this function in your loop, after that jsonObject will contain desired Json Object. I hope this is near your requirement.

Edit:

jsonObject[key] = value + parseInt(jsonObject[key] == undefined ? 0 : jsonObject[key])

Update: above snippet is adding the value, if there is duplicate key then it will parse down the corresponding value to integer and then add it to the already unique key present in the json object. So, If there is no duplicate key then it will simply add 0 else it will ad the value.

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

5 Comments

The only problem is that if there are duplicate keys, it will overrwrite the last value.
@remyabel kindly check my edit, now the function will add value to existing key's value
You should add the radix parameter to parseInt. The function should also be called something else as it doesn't 'make' an object at all, it mutates one in the outer scope.
@RobH yes you are right here it is not type safe but i answer OP's question accordingly as OP doesn't provide any code how he is dealing with requiement
Thanks, this answer did push me in the right direction, even though it did not solve it. I am not really sure what this parseInt(jsonObject[key] == undefined ? 0 : jsonObject[key]) does
0

First of all correct your object, and make keys as string:

a = {'0.24': 720, '0.1': 100}

Second: Can you show a code snippet with yhour each loop?

1 Comment

No need to correct object as long as i use the [] syntax h = {1: 100, 2: 200} h.1 => SyntaxError: Unexpected number h[1] => 100
0

Here's some code to illustrate an approach you could take:

var input = [{"factor":"0.24", "value": 240},
             {"factor":"0.1", "value": 100},
             {"factor":"0.24", "value": 240},
             {"factor":"0.24", "value": 240}];

var result = {};
for (var i = 0; i < input.length; i++) {
    var current = input[i];
    if (typeof result[current.factor] === 'undefined') {
        result[current.factor] = current.value;
    } else {
        result[current.factor] += current.value;
    }
}

console.log(result);

seen here: http://jsfiddle.net/6ZA4f/

Hopefully you can apply this idea to your own code.

Comments

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.