-4

What if I want more than 3 objects? can I loop it somehow?

let newArr = []

let newObj1 = {};  let newObj2 = {};  let newObj3 = {};

newArr.push(newObj1);
newArr.push(newObj2);
newArr.push(newObj3);

console.log(newArr);

If I add 3rd loop my array will brake, how I can substitude it? it is my full code:

let arr = [ {fullName : {surname : 'xxx', firstName : 'yyy', middleName: 'zzz'}}, {fullName : {surname : 'XXX', firstName : 'YYY', middleName: 'ZZZ'}} ] let boolObj = {fullName : {surname : true, firstName : true, middleName: false}} let loc = {"fullName.surname" : "Прізвище", "fullName.middleName" : "По-батькові"}

let newArr = [] newObj1 = {}; newObj2 = {}; newObj3 = {};

for (var i = 0; i < arr.length; i++) { let arrObjKeys = Object.keys(arr[i]); let arrObjObjKeys = Object.keys(Object.values(arr[i])[0]); let arrObjObjValues = Object.values(Object.values(arr[i])[0]); let boolObjValues = Object.values(Object.values(boolObj)[0]); let locObjKeys = Object.keys(loc); let boolObjStr1 = ${Object.keys(arr[i])}.${arrObjObjKeys[0]}; let boolObjStr3 = ${Object.keys(arr[i])}.${arrObjObjKeys[2]}; let ojbStr1 = value${i+1} if(boolObjStr1 === locObjKeys[0] && arrObjObjValues[0] !== '') { newObj1.name = loc[boolObjStr1]; newObj3.name = loc[boolObjStr3]; newObj2.name = arrObjObjKeys[1]; } for(let j = 0; j < 3; j++) { if(boolObjValues[0] === true) { newObj1[ojbStr1] = arr[i][arrObjKeys][arrObjObjKeys[j-2]]; } if(boolObjValues[1] === true) { newObj2[ojbStr1] = arr[i][arrObjKeys][arrObjObjKeys[j-1]]; } if(boolObjValues[2] === true) { newObj3[ojbStr1] = arr[i][arrObjKeys][arrObjObjKeys[j]]; } } } newArr.push(newObj1); newArr.push(newObj2); newArr.push(newObj3);

console.log(newArr);

In future to my arr I want to add object for example:

let arr = [ {fullName : {surname : 'xxx', firstName : 'yyy', middleName: 'zzz', data : {xxx: 'Boom'}}} ]

after these such code as

newObj1 = {}; newObj2 = {}; newObj3 = {}; becomes bad solution?

But if I iterate through my

newObj1 = {}; newObj2 = {}; newObj3 = {};

it will be 3 loops and my code will be broken. Pls tell how to fix it?

las vega
  • 3
  • 2

1 Answers1

1

Seems like a rather general/basic question, but why not try a simply for loop?

let  arr = [];

for (var x = 0; x < 100; x++) {
  const object = new Object();
  arr.push(object);
}
wentjun
  • 40,384
  • 10
  • 95
  • 107
  • Why invoke the constructor with `new Object()`? – CertainPerformance Jan 29 '19 at 01:49
  • Sure, there are definitely a few other ways of creating new objects, and they are well documented in many blog posts. Listing them all here would have been an overkill to a duplicated question. I am simply typing the easiest example. For the OP's benefit, do check this out! https://codeburst.io/various-ways-to-create-javascript-object-9563c6887a47 – wentjun Jan 29 '19 at 02:14
  • 1
    The easiest example would be to use an object literal instead. `const object = {};` or even `arr.push({})` alone – CertainPerformance Jan 29 '19 at 03:17
  • You are absolutely right. – wentjun Jan 29 '19 at 07:00