I want to push the unique items in an array to an empty array from the below response which is
Here the title key is same for many items, I want to push the unique items to an empty array for which I have written the following function which is :
const getPendingList = (data) => {
if (data) {
var stack = [];
for (let i = 0; i < data.length - 1; i++) {
if (data[i + 1].title !== data[i].title) {
stack.push(data[i].title);
}
}
console.log('stack', stack);
return stack.length;
}
};
But when I console stack here, I ain't getting the last unique item :
If I change the loop to
for (let i = 0; i < data.length; i++)
for some unknown reason am getting the following error:
could anyone please let me know where have I gone wrong? Any leads would be great, kindly let me know if anything else is required for better understanding.
question from:
https://stackoverflow.com/questions/65902340/how-to-create-an-array-of-unique-items-in-an-json-response-array 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…