Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

405
Views
Jquery, Find a subarray that contains a value

I would like to "push" a variable into a subarray which contains a value. This is to integrate into a "for loop". But it does not work ..:-( where is my mistake ? Thanks.

var add = "purple";
var val = "white";

arr = [
    ["blue","green","brown"],
    ["white","gray","black"],
    ["yellow","pink","red"]
];

subarr = [].indexOf(val) > -1;
arr[subarr].push(add);

/*
expected output :
arr = [
    ["blue","green","brown"],
    ["white","gray","black","purple"],
    ["yellow","pink","red"]
]
*/
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are looking

subarr = [].indexOf(val) > -1;

into an empty array.

Instead, you could find the sub array and if exists push the value to it.

const
    add = "purple";
    val = "white",
    arr = [["blue", "green", "brown"], ["white", "gray", "black"], ["yellow", "pink", "red"]],
    sub = arr.find(a => a.includes(val));

if (sub) sub.push(add);

console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years 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 vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!