0

I want to create a array like this:

[{'b':0,'c':1,'d':2},{'b':1,'c':2,'d':3},{'b':2,'c':3,'d':4}]

How can I do this in Javascript?

I have tried this:

for(i = 0; i < 3; i++){
    var b = i;
    var c = i+1;
    var d = i+2;
};
dataResult={"b":b,"c":c,"d":d};

alert(dataResult)  //not working result [{'b':0,'c':1,'d':2},{'b':1,'c':2,'d':3},{'b':2,'c':3,'d':4}] 
1
  • 1
    Writing the title of the question in UPPERCASE does not make it more important or more likely to be answered. It does rather the opposite. Commented May 30, 2018 at 6:29

6 Answers 6

2

You are just overriding value of 'b','c','d' and at the end assigning that value to 'dataResult', so you are not getting expected result.

Try this.

dataResult = [];
for(i = 0; i < 3; i++){
    dataResult.push({ 'b': i, 'c': i+1, 'd': i+2 });
};
console.log(dataResult);

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

2 Comments

I tried but could not and the result : Array [ Object, Object, Object ]
You must be printing it in alert, So use alert(JSON.stringify(dataResult)); if you want to see it in alert.
1

You'll have to create the object inside the loop, and then push it to the array:

const arr = [];
for (let i = 0; i < 3; i++) {
  var b = i;
  var c = i + 1;
  var d = i + 2;
  arr.push({ b, c, d });
}
console.log(arr);

But it would be a bit more elegant to use Array.from here:

const arr = Array.from({ length: 3 }, (_, i) => {
  const b = i;
  const c = i + 1;
  const d = i + 2;
  return { b, c, d };
});
console.log(arr);

Comments

1

Create the object inside the loop and push it to an array

var arr = [];
for (var i = 0; i < 3; i++) {
  let obj = {
    b: i,
    c: i + 1,
    d: i + 2,
  }
  arr.push(obj)
};
console.log(arr)

Comments

1

var myArr = [];

for(var i = 0; i < 3; i++){
	var data = i;
  
	myArr.push({
     b: data,
     c: data + 1,
     d: data + 2
  })
}

console.log(myArr)

4 Comments

I tried but could not and the result : Array [ Object, Object, Object ]
cause you're using alert. try to console it. you can see the result. also try to run my code snippet
but it will be used to send the parameters in ajax and the result is undefined
can you post the code on how will you send it using ajax? if it's okay with you.
1

You were creating the object outside the loop. You need to create object inside the loop.

Try following

var arr = [];
for(let i = 0; i < 3; i++){
    var b = i;
    var c = b+1; // as b = i, you can change c = b + 1
    var d = c+1; // as c = i + 1, you can change d = c + 1
    arr.push({b,c,d});
};
console.log(arr);

Comments

0

You are setting the value of b, c, d after it loops so it puts the latest value of b, c, d in dataResult. Instead, you should initialize dataResult with an empty array and push values to the array after every step of the loop

var a,b,c;
var dataResult = [];
for(i = 0; i < 3; i++){
     b = i;
     c = i+1;
    d = i+2;
dataResult.push({"b":b, "c":c, "d":d});
};

alert(dataResult);

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.