最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Returning keyvalue pair in javascript - Stack Overflow

programmeradmin1浏览0评论

I am writing a javascript program, whhich requires to store the original value of array of numbers and the doubled values in a key/value pair. I am beginner in javascript. Here is the program:

var Num=[2,10,30,50,100];
var obj = {};

function my_arr(N)  
{
    original_num = N
    return original_num;

}



function doubling(N_doubled)
{
   doubled_number = my_arr(N_doubled);

   return doubled_number * 2;
}   


for(var i=0; i< Num.length; i++)
 {
    var original_value = my_arr(Num[i]);
    console.log(original_value);
    var doubled_value = doubling(Num[i]);
    obj = {original_value : doubled_value};
console.log(obj);
}

The program reads the content of an array in a function, then, in another function, doubles the value.

My program produces the following output:

2
{ original_value: 4 }
10
{ original_value: 20 }
30
{ original_value: 60 }
50
{ original_value: 100 }
100
{ original_value: 200 }

The output which I am looking for is like this:

{2:4, 10:20,30:60,50:100, 100:200}

What's the mistake I am doing?

Thanks.

I am writing a javascript program, whhich requires to store the original value of array of numbers and the doubled values in a key/value pair. I am beginner in javascript. Here is the program:

var Num=[2,10,30,50,100];
var obj = {};

function my_arr(N)  
{
    original_num = N
    return original_num;

}



function doubling(N_doubled)
{
   doubled_number = my_arr(N_doubled);

   return doubled_number * 2;
}   


for(var i=0; i< Num.length; i++)
 {
    var original_value = my_arr(Num[i]);
    console.log(original_value);
    var doubled_value = doubling(Num[i]);
    obj = {original_value : doubled_value};
console.log(obj);
}

The program reads the content of an array in a function, then, in another function, doubles the value.

My program produces the following output:

2
{ original_value: 4 }
10
{ original_value: 20 }
30
{ original_value: 60 }
50
{ original_value: 100 }
100
{ original_value: 200 }

The output which I am looking for is like this:

{2:4, 10:20,30:60,50:100, 100:200}

What's the mistake I am doing?

Thanks.

Share Improve this question asked Jul 23, 2013 at 14:07 vamosrafavamosrafa 6955 gold badges12 silver badges35 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Your goal is to enrich the obj map with new properties in order to get {2:4, 10:20,30:60,50:100, 100:200}. But instead of doing that you're replacing the value of the obj variable with an object having only one property.

Change

obj = {original_value : doubled_value};

to

obj[original_value] = doubled_value;

And then, at the end of the loop, just log

console.log(obj);

Here's the plete loop code :

for(var i=0; i< Num.length; i++) {
    var original_value = my_arr(Num[i]);
    var doubled_value = doubling(original_value);
    obj[original_value] = doubled_value;
}
console.log(obj);

You can't use an expression as a label in an Object literal, it doesn't get evaluated. Instead, switch to bracket notation.

var original_value = my_arr(Num[i]),
    doubled_value = doubling(Num[i]);
obj = {}; // remove this line if you don't want object to be reset each iteration
obj[original_value] = doubled_value;

Or:

//Original array
var Num=[2,10,30,50,100];
//Object with original array keys with key double values
var obj = myFunc(Num);
//Print object
console.log(obj);


function myFunc(arr)
{
    var obj = {};
    for (var i in arr) obj[arr[i]] = arr[i] * 2;
    return obj;
}
发布评论

评论列表(0)

  1. 暂无评论