0

I want to double the key values in an array of objects and I always end up getting the last key:val in the list. I want to be able to print the whole array of objects doubled their values.

doubleArr = [];

function doubleSize (a,b) {
    for(var i in a) {
        if (a.hasOwnProperty(i)) {
            var doubleObj = {};
            doubleObj[i] = b * a[i];
            var results = doubleObj[i];
            doubleArr.push(results);

        }

    }

    return doubleObj;
}

console.log(doubleSize({1:1,1:2,1:3,1:4,1:5}, 2))

I only get {1:10}. The goal is to get {1:2,1:4,1:6,1:8,1:10}. Thanks.

Michael Stokes
  • 401
  • 1
  • 10
  • 24

4 Answers4

0

You are using the same key for your objects 1. I am assuming that you don't need objects as they don't provide anything obvious to your code:

Edit If you insist on using objects, I have provided the second version also.

function doubleSize (a,b) 
{
    var doubleArr = [];
    for(var i=0; i<a.length;i++) 
    {
        doubleArr.push(a[i]*b);
    }

    return doubleArr;
}

console.log(doubleSize([1,2,3,4,5], 2))

function doubleSize2 (a,b) 
{
    var result = {};
    for(var i in a)
    {
        result[i] = a[i]*b
    }

    return result;
}

console.log(doubleSize2({a:1,b:2,c:3,d:4,e:5}, 2))
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
0

The primary issue with your code is that you cannot have duplicate keys in an object. Every key in an object must be unique; otherwise, you are essentially redeclaring the value associated with that key over and over (so, naturally, in your example, the key 1 will end up being associated with the last value you assign it: 5).

However, even if you used an object without duplicate keys, you still have the following issues:

  • Your doubleArr should not be declared in the global scope, but instead within the function; moreover, you don't need an array for what you're doing
  • Your code is actually constructing an array of objects rather than an object. If you want to return an object, you should build up an object with unique values in your function.

Here's an example of a modified version of your code:

function doubleSize (a,b) {
    var doubleObj = {};
    for(var i in a) {
        if (a.hasOwnProperty(i)) {
            doubleObj[i] = b * a[i];
        }
    }

    return doubleObj;
}

console.log(doubleSize({1:1,2:2,3:3,4:4,5:5}, 2)) // Note the unique keys

However, I don't fully understand the need for objects. You could use arrays instead, and then replace all of your code with a simple one-liner using Array.map:

var arr = [1, 2, 3, 4, 5]
var doubledArr = arr.map(x => 2*x) // An arrow function that multiplies each value in the array by 2
console.log(doubledArr)
aaplmath
  • 1,717
  • 1
  • 14
  • 27
0

You can't have duplicate keys in an object, you can have an array of objects with the same key though. Something like:

function doubleSize (a, b) {
  var out = [];
  for(var i = 0; i < a.length; i++) {
    out.push({'1': a[i]['1'] * b});
  }

  return out;
}

console.log(doubleSize([{'1': 1}, {'1': 2}, {'1': 3}, {'1': 4}, {'1': 5}], 2));
Max
  • 469
  • 4
  • 7
0
function doubleSize (a,b) {
    var midObject = {};
    for(var i in a) {
        if (a.hasOwnProperty(i)) {
            midObject[i] = b * a[i];
        }
    }

    return midObject;
}

console.log(doubleSize({1:1,2:2,3:3,4:4,5:5}, 5))