1

How can I generate such data using a loop in JavaScript?

 data: [{
     y: 'bjjjjjjj mmnmmanf j',
     a: 98,
     b: 48

 }, {
     y: 'd',
     a: 50,
     b: 40
 }, {
     y: 'e',
     a: 75,
     b: 65
 }, {
     y: 'f',
     a: 50,
     b: 40
 }, {
     y: 'g',
     a: 75,
     b: 65
 }, {
     y: 'h',
     a: 100,
     b: 90
 }],

So far I've stored the data in the different arrays, but have no idea how can I generate the structure:

[{y:'d',a:98,b:48},{y:'d',a:50,b:40}, etc...]

Here are my arrays:

I_array = [98,50,75,50,75,100];
C_array = [48,40,46,40,65,90];
Name_array = ['b','d','e','f','g','h'];
5
  • 1
    Why are the values in separate arrays to begin with if you need them in the array-of-objects structure? Commented Apr 29, 2015 at 12:00
  • there are separate and you must make them one Commented Apr 29, 2015 at 12:09
  • 1
    I must, must I? So you aren't able to save yourself the conversion step by generating the right structure in the first place? Commented Apr 29, 2015 at 12:15
  • sorry to use "you" I must make them one Commented Apr 29, 2015 at 12:33
  • @ReginwaldtLed you can never guarantee the quality of the data you get by "making them one". It will be subject to many errors afterwards. Commented Apr 29, 2015 at 12:36

4 Answers 4

4

Try this: This is just an example. You can loop it according to your requirements.

var data = [];
for(var i = 0;i<4;i++)                   //your for loop starts here
{
    var obj = {};
    obj.y ='y';
    obj.a='a';
    obj.z='e';
    data.push(obj);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Could also just do data.push( {"y": y, "a":a, "z":z} );
@DelightedD0D Yes, but i think it will be more easy for anyone to understand.
3

You could map it using javascript array.prototype.map:

var I_array = [98, 50, 75, 50, 75, 100];
var C_array = [48, 40, 46, 40, 65, 90];
var Name_array = ['b', 'd', 'e', 'f', 'g', 'h'];

var newArray = Name_array.map(function (o, i) {
    return {
        y: o,
        a: I_array[i],
        b: C_array[i]
    };
});

-jsFiddle-

Comments

1

var I_array = [98,50,75,50,75,100],
    C_array = [48,40,46,40,65,90],
    Name_array = ['b','d','e','f','g','h'],
    data = $.map( I_array, function(v, i) {
      return {        
        y: Name_array[i],
        a: v,
        b: C_array[i]
      };
    });

console.log( data );
$('body').text( JSON.stringify( data ) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Comments

0

You can try:

I_array = [98, 50, 75, 50, 75, 100];
C_array = [48, 40, 46, 40, 65, 90];
Name_array = ['b', 'd', 'e', 'f', 'g', 'h'];

var s = "";

for (i = 0; i < I_array.length; i++) {
    if (i > 0) s += ",";
    s += "{ \"y\":\"" + Name_array[i] + "\", \"a\":" + I_array[i] + ", \"b\": " + C_array[i] + " }";
}

var text = "{\"data\": [";
text += s;
text += "]}";

var obj = JSON.parse(text);

jsfiddle

2 Comments

Why would you build up a string and then parse it when you can just build an array of objects directly?
Maybe he doesn't even need to parse it? In the question it wasn't specified.

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.