-1

I'm learning by coding, here i have 'allNodes' an array of objects and 'currentNodee' object, i want to check if allNodes contains 'analyser' and 'id' which is equal to 'analyser' and 'id' of 'currentNodee' then take 'analyser' value and print it, but if for example 'allNodes' has same id but its analyser is 'undefined' then do not print anything.

what am i doing wrong here ? any idea is appreciated

  const allNodes = [
    { analyser: undefined, id: 7 },
    { analyser: "abc", id: 6 },
  ];

  const currentNodee = { analyser: "abc", id: 7 };

   console.log(
    allNodes.find((option: any) => {
      option.id === currentNodee.id &&
        option?.analyser === currentNodee?.analyser;
    })
  );

English is not my mother language so could be mistakes.

4
  • Be aware that {} === {} // false Commented Jun 2, 2022 at 21:28
  • Does this answer your question? Comparing objects in two arrays Commented Jun 2, 2022 at 21:28
  • @evolutionxbox that is comparing two arrays with their object, here is an array of objects and another just object Commented Jun 2, 2022 at 21:31
  • Same underlying issue Commented Jun 2, 2022 at 21:50

2 Answers 2

0

Presented below is one possible way to achieve the desired objective.

Code Snippet

const rd = document.getElementById("rd");
const btn = document.getElementById("myBtn");
const btn2 = document.getElementById("myBtn2");

const allNodes = [{
    analyser: undefined,
    id: 7
  },
  {
    analyser: "abc",
    id: 6
  },
];

const currentNodee = {
  analyser: "abc",
  id: 7
};

const currentNodef = {
  analyser: "abc",
  id: 6
};

const getResult = (curr) => {
  const tgt = allNodes.find(
    ({ analyser, id }) => curr.id === id
  );
  if (tgt) {
    if (
      tgt.analyser &&
      tgt.analyser === curr.analyser
    ) {
      return JSON.stringify(tgt);
    } else {
      return "print nothing";
    }
  } else {
    return "not found"
  };
};

btn.textContent = 'Search analyser id=7';
btn.addEventListener('click', () => {
  rd.innerText = getResult(currentNodee);
});

btn2.textContent = 'Search analyser id=6';
btn2.addEventListener('click', () => {
  rd.innerText = getResult(currentNodef);
});
<div>
Search result: <div id="rd">empty</div>
<button id="myBtn" />
<button id="myBtn2" />
</div>

Printing in single line can be done using JSON.stringify() as demonstrated in the above code snippet.

Sign up to request clarification or add additional context in comments.

1 Comment

I have added "print nothing" only as a placeholder; instead, if we simply put "" - it will work as well.
0

const allNodes = [
    { analyser: undefined, id: 7 },
    { analyser: "abc", id: 6 },
  ];

  const currentNodee = { analyser: "abc", id: 7 };
  
  const result = allNodes.find(node => (node.analyser === currentNodee.analyser) && (node.id === currentNodee.id))

  console.log(!result ? 'No result found' : result);


1 Comment

how to do printing in one line for example if there is some analyser then do this, like this node.analyser ? <h1>{node.analyser} </h1>:null

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.