krish
krish

Reputation: 1117

Pushing objects into array - returing duplicate results

I am just trying to push 2 different columns(name and sys_id) data into an array. I have totally 38 records with different data some duplicates also there.

When I try to insert as objects its inserting only one record details 38times.

0: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
1: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
2: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
3: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
4: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
5: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
6: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
7: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
8: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
9: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
10: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}
11: {name: "Incident Request", sys_id: "3ee543f61b363740f713ed7b2f4bcbc0"}

I have tried below script ,in item I will be getting all record details.

var arr=[];
var obj = {};

while(item.next()){
    obj["name"] = item.cat_item.name.toString();
    obj["sys_id"] = item.cat_item.sys_id.toString();
    arr.push(data.obj);
}

if I push only item.cat_item.name.toString() into an array directly am getting correct data.

0: "Manage AAA Devices"
1: ""
2: "Manage AAA - ISE Admin Functions"
3: "Manage AAA Devices"
4: "Manage AAA - ISE Admin Functions"
5: "SNOW Change Request"
6: ""
7: "Manage AAA - ISE Admin Functions"
8: "Manage AAA - ISE Admin Functions"
9: "Incident Request"
10: "Manage AAA - ISE Admin Functions"
11: "Incident Request"
12: "Incident Request"
13: "Server Lifecycle(EOL)"
14: "Incident Request"
15: "Server Lifecycle(EOL)"
16: "Incident Request"
17: "Incident Request"

only issue is while inserting as object. Is there anything wrong with my code? Can anyone help me?

Upvotes: 0

Views: 1038

Answers (3)

Danziger
Danziger

Reputation: 21191

You need to create a new obj object for each iteration, so its declaration and initialization should go inside the loop:

const data = [
  { name: "Incident Request", sys_id: 1 },
  { name: "Incident Request", sys_id: 2 },
  { name: "Incident Request", sys_id: 1 },
  { name: "Incident Request", sys_id: 4 },
  { name: "Incident Request", sys_id: 2 },
  { name: "Incident Request", sys_id: 1 },
  { name: "Incident Request", sys_id: 2 },
  { name: "Incident Request", sys_id: 3 },
];

const arr = [];

data.forEach((item) => {
  const name = item.name.toString();
  const sys_id = item.sys_id.toString();
  
  // Check if there's already an entry for this `sys_id` and do nothing in that case:
  if (arr.find(cursor => cursor.sys_id === sys_id)) return;
  
  // Here we are not reusing a single object, but creating a new one on each iteration:
  arr.push({ name, sys_id });
});

console.log(arr.map(({ name, sys_id }) => `${ name } - ${ sys_id }`));
.as-console-wrapper {
  max-height: 100% !important;
}

Otherwise, you are always updating the same object and adding it to an additional position in arr. As all positions are referencing the same object, each update you do on it affects all of them, so you will only see the values of the last iteration repeated multiple times:

const data = [
  { name: "Incident Request", sys_id: 1 },
  { name: "Incident Request", sys_id: 2 },
  { name: "Incident Request", sys_id: 3 },
  { name: "Incident Request", sys_id: 4 },
  { name: "Incident Request", sys_id: 5 },
  { name: "Incident Request", sys_id: 6 },
  { name: "Incident Request", sys_id: 7 },
  { name: "Incident Request", sys_id: 8 },
];

const arr = [];
const obj = {};

data.forEach((item) => {
  // Update the existing object (always the same object):
  obj.name = item.name.toString(),
  obj.sys_id = item.sys_id.toString(),

  // And add it one more time to the array:
  arr.push(obj);
});

console.log(arr.map(({ name, sys_id }) => `${ name } - ${ sys_id }`));
.as-console-wrapper {
  max-height: 100% !important;
}

Upvotes: 1

deviloper
deviloper

Reputation: 7240

The next() method returns an object with two properties: value, done

var arr=[];
var obj = {};
var index = item.next();

while (!index.done) {
    obj["name"] = index.cat_item.name.toString();
    obj["sys_id"] = index.cat_item.sys_id.toString();
    arr.push(data.obj);
    index = item.next();
}

Upvotes: 1

k.kolev
k.kolev

Reputation: 93

You are referencing the same object in every iteration and just changing it's attributes. Try creating a new object inside the while loop.

var arr=[];

while(item.next()){
    var obj = {};
    obj["name"] = item.cat_item.name.toString();
    obj["sys_id"] = item.cat_item.sys_id.toString();
    arr.push(data.obj);
}

Upvotes: 1

Related Questions