+ 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?
13 Réponses
+ 1
You have 2 arrays, items and id, what are you trying to calculate with them? If i dont know i cant help..
+ 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"]
+ 1
Insider Evil hang on, i accidentally deleted my code.. have to rewrite it 🥴
+ 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 😅🙏
+ 1
Insider Evil I have updated the code to include another speculation of what you may mean 😛
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
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
0
Giorgos D that's old one and now I modified that, now got error some time with 'pr'
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);
0
Giorgos D what about "pos" object in Data?
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