Instead of [ new Array() ]

Joined
Jul 9, 2023
Messages
25
Reaction score
0
Hello
I have to use the code below.
I don't want to use [ new Array() ] . <== new memory allocation
I need to replace [ new Array() ] in the code below.
Is there a way other than [ new Array() ] ?

Code:
var colval = new Array();
for (i=0; i<each.length; i++)
{
    var ID = each[i].ID.tostring();
    colval[ID] = new Array();
}

for (i=0; i<each.length; i++)
{
    ID = each[i].ID;
    NAME = each[i].NAME;
    colval[ID].push(NAME);
}
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title></title>
    <style>
    
    </style>
  
  </head>
  <body>
      <input type="text" id="i_1" name="input1" value="">
    <input type="text" id="i_2" name="input2" value="">
    <input type="text" id="i_3" name="input3" value="">
    <input type="text" id="i_4" name="input4" value="">
    <input type="text" id="i_5" name="input5" value="">
    <script>
    let o = {};
    document.querySelectorAll('[id^=i_]').forEach( el => o[el.id] = el.name );
    console.log( o ); // {i_1: 'input1', i_2: 'input2', i_3: 'input3', i_4: 'input4', i_5: 'input5'}
    </script>
  </body>
</html>
 
Joined
Jul 9, 2023
Messages
25
Reaction score
0
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title></title>
    <style>
   
    </style>
 
  </head>
  <body>
      <input type="text" id="i_1" name="input1" value="">
    <input type="text" id="i_2" name="input2" value="">
    <input type="text" id="i_3" name="input3" value="">
    <input type="text" id="i_4" name="input4" value="">
    <input type="text" id="i_5" name="input5" value="">
    <script>
    let o = {};
    document.querySelectorAll('[id^=i_]').forEach( el => o[el.id] = el.name );
    console.log( o ); // {i_1: 'input1', i_2: 'input2', i_3: 'input3', i_4: 'input4', i_5: 'input5'}
    </script>
  </body>
</html>

Hello.
Instead of document.querySelectorAll
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
Have you tried e.g. like this way?
JavaScript:
var each = [
    { ID: 1, NAME: 'Alice' },
    { ID: 2, NAME: 'Bob' },
    { ID: 3, NAME: 'Charlie' },
    { ID: 4, NAME: 'David' }
];

var colval = {};

for (let i=0; i<each.length; i++) {
    const ID = each[i].ID.toString();
    colval[ID] = [each[i].NAME];
}

console.log(colval);
 
Joined
Jul 9, 2023
Messages
25
Reaction score
0
Have you tried e.g. like this way?
JavaScript:
var each = [
    { ID: 1, NAME: 'Alice' },
    { ID: 2, NAME: 'Bob' },
    { ID: 3, NAME: 'Charlie' },
    { ID: 4, NAME: 'David' }
];

var colval = {};

for (let i=0; i<each.length; i++) {
    const ID = each[i].ID.toString();
    colval[ID] = [each[i].NAME];
}

console.log(colval);

var each = [
{ ID: 1, NAME: 'Alice' },
{ ID:1, NAME: 'CCC'},
{ ID:1, NAME:'DDD'},
{ ID: 2, NAME: 'Bob' },
{ ID:2, NAME:'EEE'},
{ ID:2, NAME:'DDD'},
{ ID: 3, NAME: 'Charlie' },
{ ID: 3, NAME: 'RRR' },
{ ID: 3, NAME: 'QQQ' },
{ ID: 4, NAME: 'David' }
{ ID:4, NAME:'HHHH'},
{ ID:4, NAME:'UUU'},
];
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
Okay, now i understand your point, how about this?
[ on-line ]
JavaScript:
const each = [
  { ID: 1, NAME: 'Alice1' },
  { ID: 1, NAME: 'Bob' },
  { ID: 1, NAME: 'Charlie' },
  { ID: 2, NAME: 'David' },
  { ID: 2, NAME: 'Eva' },
  { ID: 2, NAME: 'Frank' },
  { ID: 2, NAME: 'Grace' },
  { ID: 3, NAME: 'Jasmine' },
  { ID: 3, NAME: 'Kevin' },
  { ID: 4, NAME: 'Hannah' },
  { ID: 4, NAME: 'David' },
  { ID: 4, NAME: 'Michael' }
];

const objobj = {};

for (const item of each) {
  const ID = item.ID;

  if (! objobj[ID]) objobj[ID] = [];
  objobj[ID].push(item.NAME);
}

const objarr = Object.values(objobj);

console.log(objarr);
console.log(objobj);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top