0

What I am trying to do is to create an array, whenever the element I am looking for is found (bank in my case). The first occurrence of the element should be placed in the first array and the second one in the second array and so on. Finally after all the elements are placed in separate arrays I push them onto a main array.

Is it possible to create the arrays with different names dynamically like Bank_1 ,Bank_2 and so on ?? and is this the right approach ?

var bankcontainer = [];
var bank = [];
for (let i = 0; i < length ;i++)
{
 let bankname = data.periods[0].decisions[i].bank;
 bank[i] = [];
 bank[i].push(bankname);
 bankcontainer.push(bank);
}

Example:

// Input: 
[{ bank: "team1" }, { bank: "team2" }, { bank: "team3" }]
// Result:
{ bank_1: ["team1"], bank_3: ["team3"], bank_3: ["team3"] }
kornieff
2,57722 silver badges29 bronze badges
asked Nov 23, 2019 at 18:08
11
  • bank[i] = []; is wrong. Commented Nov 23, 2019 at 18:11
  • I know, I am trying to improve this. Any suggestions ? Commented Nov 23, 2019 at 18:12
  • give example of and array u want Commented Nov 23, 2019 at 18:14
  • Please provide sample data and outcome you are expecting. Commented Nov 23, 2019 at 18:15
  • 1
    @CliveCharles, good luck, see how answers just pop after problem got defined with sample data. Brush up on ES6 -- for loops are so 20th century :P. Commented Nov 23, 2019 at 18:57

7 Answers 7

3

const data = [{ bank: "team1" }, { bank: "team2" }, { bank: "team3" }]
console.log(
 Object.fromEntries(
 data.map(
 (v, i) => [`bank_${++i}`, v.bank]
 )
 )
)

answered Nov 23, 2019 at 18:41
Sign up to request clarification or add additional context in comments.

Comments

1

Iterate the array with Array.map() and create an entry of [key, value] from each item, and then convert to an object with Object.fromEntries():

const data = [{ bank: "team1" }, { bank: "team2" }, { bank: "team3" }]
const result = Object.fromEntries(
 data.map((o, i) => [`bank_${i + 1}`, o.bank])
)
console.log(result)

If Object.fromEntries() is not supported, you can use Array.reduce():

const data = [{ bank: "team1" }, { bank: "team2" }, { bank: "team3" }]
const result = data.reduce((r, o, i) => {
 r[`bank_${i + 1}`] = o.bank;
 return r;
}, {})
console.log(result)

answered Nov 23, 2019 at 19:00

Comments

0

this will create bank names dynamically

var bankcontainer = [];
var bank = [];
for (let i = 0; i < length ;i++)
{
 let bankname = data.periods[0].decisions[i].bank;
 const bank["bank_" + i] = [];
 bank["bank_" + i].push(bankname);
 bank = bank["bank_" + i];
}
bankcontainer.push(bank);
answered Nov 23, 2019 at 18:12

3 Comments

wouldn't all the elements go into the same array ?
Please comment your code so the person you are trying to help can understand what the code does and how it solves their problem.
Please comment your code so the person you are trying to help can understand what the code does and how it solves their problem.
0

Yes it is possible:

const myBanks = {};
const banks = ['BofA', 'Goldman Sachs', 'Stackoverflow Credit Union', 'Chase', 'Captial One 360'];
for (let i = 0; i < banks.length; i++)
{
 myBanks['Bank_' + i] = [banks[i]];
}
console.log(myBanks);

answered Nov 23, 2019 at 18:47

1 Comment

Thanks for answering my question: Now how can i append/push something on index 2 for example in "bank_1" ?
0

const input = [{ bank: "team1" }, { bank: "team2" }, { bank: "team3" }];
const result = {};
input.forEach(
 (item, i) => { 
 result[`bank_${i +1}`] = [item.bank]; 
 }
);
console.log(result);

answered Nov 23, 2019 at 18:47

Comments

0

The map() can be used here to manipulate the object as follows:

let data = [{ bank: "team1" }, { bank: "team2" }, { bank: "team3" }];
newData = data.map((item, index) => {
return {['bank_' + index] : [item.bank]}
})
console.log(newData);

answered Nov 23, 2019 at 18:51

Comments

0

Run forEach on data and create a dynamic key using the index, see below:

 let data = [{ bank: "team1" }, { bank: "team2" }, { bank: "team3" }];
 let newData = {};
 data.forEach((item, index) => {
 newData['bank_' + (index+1)] = [item.bank];
 })
 console.log(newData);

answered Nov 23, 2019 at 19:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.