+ 2

Can't read property of object

//Note:- Items is array & Id is also array function searchItems(id){ var data = []; items.forEach( function(element,index,array){ for(var x = 0; x < id.length; x++){ if(element.title.toLowerCase().includes(id[x])){ var it = index; if(data.some((o) => o.pos === it)) { try{ data[index].pr = ++data[index].pr; }catch(e){ console.log(e); } } else { data.push({pos:index,pr:0}); } } } }); // At position of data[index].pr = ++data[index].pr; in code, it cause error "can't read property of pr" But some times it not throw any error why? I defined pr at data.push({pos:index,pr:0}); How to fix?

14th Aug 2021, 3:25 PM
Insider Evil
Insider Evil - avatar
13 odpowiedzi
+ 1
You have 2 arrays, items and id, what are you trying to calculate with them? If i dont know i cant help..
14th Aug 2021, 4:20 PM
Giorgos
+ 1
Giorgos D in items I have list of articles contains key title and description where as in id it contains single data array of few words which I want to match with article title items = [{"title":"how to cook food","description":".."}] Id = ["cook","food"]
14th Aug 2021, 4:24 PM
Insider Evil
Insider Evil - avatar
+ 1
Insider Evil hang on, i accidentally deleted my code.. have to rewrite it 🥴
14th Aug 2021, 4:36 PM
Giorgos
14th Aug 2021, 4:43 PM
Giorgos
+ 1
Giorgos D extra note that pr is stand for page rank and pos stand for index of items position which matched with id And all I collect in data array 😅🙏
14th Aug 2021, 4:44 PM
Insider Evil
Insider Evil - avatar
+ 1
Insider Evil I have updated the code to include another speculation of what you may mean 😛
14th Aug 2021, 4:57 PM
Giorgos
0
Insider Evil What are you trying to achieve? What should searchItems() do? Also, it looks like 'data' should be a dictionary instead of array
14th Aug 2021, 4:01 PM
Giorgos
0
Insider Evil In the code in the other question: replace data.push(element.title) with data.push(index) and you will have the indices of items array instead
14th Aug 2021, 4:08 PM
Giorgos
0
Giorgos D that's old one and now I modified that, now got error some time with 'pr'
14th Aug 2021, 4:10 PM
Insider Evil
Insider Evil - avatar
0
Insider Evil is this what you want? If not then provide some more info about what should searchItems do function searchItems(id){ var data = {}; //dictionary items.forEach( function(element,index,array){ for(var x = 0; x < id.length; x++){ if(element.title.toLowerCase().includes(id[x])){ if(data[index]) { data[index]++; //pr++ } else { data[index] = 0; //pr to 0 } } } }); console.log(data);
14th Aug 2021, 4:16 PM
Giorgos
0
Giorgos D what about "pos" object in Data?
14th Aug 2021, 4:18 PM
Insider Evil
Insider Evil - avatar
0
Giorgos D in items I have few articles contains key title and description Like items = [{"title":"how to cook food","description":"...."}] and in id, id = ["cook","food"] and I want match if title contains id each index value
14th Aug 2021, 4:28 PM
Insider Evil
Insider Evil - avatar