Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

58
Views
Filter similar names in object array JavaScript

so here is a problem I've been struggling with for a little bit now. I want to filter a list of objects but the code seems to work only for arrays. I can i reach the same result with objects? Basically what I want is something like: if card.name = jose. then remove jose._R if card.name = jose._R then remove jose.

Code:

var data = {'cards' : 
    [
    {'name': "jose."}, 
    {'name': "jose._R"}, 
    {'name': "rodrigo."}, 
    {'name': "rodrigo._R"}, 
    {'name': "jojo."}, 
    {'name': "jojo._R"},
    ]
    };
    
let dataName = data.cards[0].name;
let rev = data.cards[0].name + "_R";

var data2 = ["jose.", "jose._R", "rodrigo.", "rodrigo._R", "jojo.", "jojo._R",];
let dataName2 = data2[0];
let rev2 = data2[0] + "_R";


if(dataName2.endsWith('.')){
    data2 = data2.filter(function(f) {return f !== rev2});
};

if(dataName.endsWith('.')){
    data = data.cards.filter(function(f) {return f !== rev}); // NOT WORKING! It doesn't remove the object array... :-(
};

console.log(data);
console.log(data2);
7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

In your filter function

function(f) { 
  return f !== rev; 
}

You are comparing the entire object { 'name': 'rodrigo.' } to the string so they will never be equal. Instead you should compare the name property with the given string:

function(f) {
  return f.name !== rev;
}
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.