Good night guys.

There is a small question.

There is a function that cleans an array of objects.

I transfer the result of this function further along the chain .then and after the transfer the search of this object stops working.

What is the reason tell me who knows.

const remDupl = function (arr, fild) { var rez = {news:[]}; var lookupObject = {}; for(var i in arr) { lookupObject[arr[i][fild]] = arr[i]; } for( i in lookupObject) { rez.news.push(lookupObject[i]); } return rez; }; 

as you can see further, everything works well here.

 const remDupl = function (arr, fild) { var rez = {news:[]}; var lookupObject = {}; for(var i in arr) { lookupObject[arr[i][fild]] = arr[i]; } for( i in lookupObject) { rez.news.push(lookupObject[i]); } return rez; }; let arr={ news : [ {'title': 'one'}, {'title': 'two'}, {'title': 'three'}, {'title': 'four', 'name': 'dima'}, {'title': 'four', 'name': 'alex'}, {'title': 'one', 'name': 'alex'}, ] }; let rec = remDupl(arr.news, 'title'); console.log(rec); 

However, the situation is worse when the result of this function is interrupted further through the promises.

Why is it added? Inex 0 and everything would be fine but even about this address by searching through forEach results --- tat Void

enter image description here

Here is the test of this function.

 let rec = remDupl(arr.news, 'title'); console.log(rec); 

enter image description here

This is how the object looks after the transfer of promis

 console.log(cleanArr); 

Apparently added index 0 from where? And why brute force does not work on it?

the conclusion of the previous promis.

enter image description here

  .then(pulledNews => { return remDupl(pulledNews, 'title'); }) .then(cleanArr => { // console.log(cleanArr); cleanArr.news[0].forEach(item=>{console.log(item)}); }) 

enter image description here

  • Show function which returns promis. - Lev Shportak
  • @LevShportak added at the end, see - dpi

1 answer 1

Did you want something like that? -

 const remDupl = function (arr, fild) { var rez = {news:[]}; var lookupObject = {}; for(var i in arr) { lookupObject[arr[i][fild]] = arr[i]; } for( i in lookupObject) { rez.news.push(lookupObject[i]); } return rez; }; new Promise(function(resolve, reject) { let arr = { news: [ { 'title': 'one' }, { 'title': 'two' }, { 'title': 'three' }, { 'title': 'four', 'name': 'dima' }, { 'title': 'four', 'name': 'alex' }, { 'title': 'one', 'name': 'alex' } ] }; resolve(arr); }) .then(pulledNews => remDupl(pulledNews.news, 'title')) .then(cleanArr => cleanArr.news.forEach(item => console.log(item))); 

  • everything seems normal and what confuses you ?? - dpi
  • Added screen output as you requested. - dpi