0

enter image description here

enter image description here

I have simply iterated through an array of objects. But I have no clue how I should compare the previous object values and display data along with current data. as shown in the image.

My Half solutions:

const dataSet = [{
    categoryId: 100,
    parentCategoryId: -1,
    name: "Business",
    keyword: "Money",
  },
  {
    categoryId: 200,
    parentCategoryId: -1,
    name: "Tutoring",
    keyword: "Teaching",
  },
  {
    categoryId: 101,
    parentCategoryId: 100,
    name: "Accounting",
    keyword: "Taxes",
  },
  {
    categoryId: 102,
    parentCategoryId: 100,
    name: "Taxation",
    keyword: "",
  },
  {
    categoryId: 201,
    parentCategoryId: 200,
    name: "Computer",
    keyword: "",
  },
  {
    categoryId: 103,
    parentCategoryId: 101,
    name: "Corporate Tax",
    keyword: "",
  },
  {
    categoryId: 202,
    parentCategoryId: 201,
    name: "Operating System",
    keyword: "",
  },
  {
    categoryId: 109,
    parentCategoryId: 101,
    name: "Small Business Tax",
    keyword: "",
  }
]

function solution(X) {
    // search your keyword 
    
    dataSet.map((item)=>{
        console.log("Item List: ", item);
        if (X === item.categoryId){
            const displayData = `\n\t ParentCategoryId : ${item.parentCategoryId} \n\t Name : ${item.name} \n\t Kewords : ${item.keyword}`;
           try{
               if(displayData) {
                   console.log("Your Searched Data:", displayData);
               }
           }catch (e) {
               return console.log ("error:", e);
           }
        }
    })
}

solution(201);

2 Answers 2

1

Below method will solve your problem.

function solution(cId){
    let result = null;
    const getParentNode = function(parentId){
        const parentNode = dataSet.find(elm => elm.categoryId === parentId);
        if(parentNode && parentNode.keyword === ""){
            return getParentNode(parentNode.parentCategoryId);
        }
        return parentNode;
    }
    for(let i=0; i<dataSet.length; i++){
        if(dataSet[i].categoryId === cId){
            if(dataSet[i].keyword === "")
                result = {...dataSet[i], keyword: getParentNode(dataSet[i].parentCategoryId).keyword};
            else
                result = dataSet[i];
            break;
        }
    }
    return result;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or an explanation of your approach, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
It worked !! thanks a lot, it resolved the parent keyword search. :)
1

This may probably help you!

var result = result1.filter(function (o1) {
    return result2.some(function (o2) {
        return o1.id === o2.id; // return the ones with equal id
   });
});
// if you want to be more clever...
let result = result1.filter(o1 => result2.some(o2 => o1.id === o2.id));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.