1

If I have two array

A = [ "B,C,D"
      "W,F,G"
      "M,S,E"
    ]
D = [E]

and want to compare D OR G with E (I mean the third one in each line)and If they are equal can show all the line for me. The third one in each line a part of each line and I want just compare the third one If it is equal show that line have equal variable . I wrote this code but didn't give me the response that I need have :

 for(var k=0 ; k<A.length ; k++){
    if(A[k].split(",")[2] === E) {
       finalSuccessServices = A;
       console.log(finalSuccessServices );
     }

How can I improve my code to get that line that have my equal variable?

6
  • what is E? Why are you setting finalSuccessServices = A, when A never changes anyways? Did you mean A[k]? Commented Feb 18, 2020 at 15:54
  • what do you mean with line? please add the wanted result. Commented Feb 18, 2020 at 15:55
  • could you please give me the expected output ? Commented Feb 18, 2020 at 15:58
  • @ ASDFGerte exactly A[k] yes Commented Feb 18, 2020 at 16:02
  • @Nina Scholz for example in One line I have "B,C,D" Commented Feb 18, 2020 at 16:03

4 Answers 4

1

You can use find() to get the matching string, provided you have a single match:

const A = [ "B,C,D",
      "W,F,G",
      "M,S,E"
    ];
const D = ['E'];

let result = A.find(str => str.split(',')[2] === D[0]);

console.log(result)

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

Comments

0

You could filter the array and have a look up with includes.

var array = ["B,C,D", "W,F,G", "M,S,E"],
    pattern = ['E'],
    result = array.filter(s => pattern.includes(s.split(',')[2]));

console.log(result);

If you need only the first one, take find.

var array = ["B,C,D", "W,F,G", "M,S,E"],
    pattern = ['E'],
    result = array.find(s => pattern.includes(s.split(',')[2]));

console.log(result);

12 Comments

what's the meaning of s => pattern.includes(s.split(',')[2])
when You use s=> what is the meaning ?
it's an arrow function, a short form of function () {} with some special features.
Thanks a lot for your help
It is makesense for if not equal yes ?
|
0

You can use the function filter along with the function includes in order to select those lines which have a third letter within the array D.

let A = [ "B,C,D", "W,F,G", "M,S,E" ],
    D = ["E"],
    result = A.filter(s => {
      let [,,C] = s.split(",");
      return D.includes(C);
    });
    
console.log(result);

Comments

0
  1. Use split and destructure to get third element
  2. Compare third value and first (from array D)
  3. Console log and push to finalSuccessServices array.

const A = ["B,C,D", "W,F,G", "M,S,E"];
const D = ["E"];

const [first] = D;
const finalSuccessServices = [];
A.forEach(line => {
  const [,, third] = line.split(",");
  if (third === first) {
    console.log(line);
    finalSuccessServices.push(line);
  }
});

console.log("finalSuccessServices", finalSuccessServices);

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.