1

I get the value of "name" from a promise and I store it in an array as it MAY contain multiple names.

var name = ["Arun_Manohar"];
var combinations = ["abc", "def", "ghi", "har", "lmn"];

Lets say I have an activities object.

var activities = {
 driving: false,
 jogging: false,
 drinking: false
}

How do I go about writing a check to update the activities object in this case. NOTE: No two elements in combinations array will be a substring of names array. Just one.

Case: Since name contains a substring of one of the elements of combinations, I need to update the activities object. If the name contains a substring like "abc", I will update the object with different values.

Use of lodash would be great.

Tried something and wasnt able to update the object.

if(name.indexOf(_.filter(combinations, function(e) { return.indexOf("har") !== -1})) !== -1) {
 activities.driving= true;
 activities.jogging= true;
}

If the name contains "abcpoc", since the substring is present in combinations array, I update the object with a different set of values.

P.S.
16.4k14 gold badges65 silver badges86 bronze badges
asked Sep 28, 2017 at 20:55
7
  • 2
    I don't understand, what does the activities object have to do with the two arrays? Commented Sep 28, 2017 at 20:56
  • Its nothing related to do with it. I just want to know how it might work. Commented Sep 28, 2017 at 20:57
  • You say you want to update the activities object. Update it with what? Commented Sep 28, 2017 at 20:58
  • Loop through the two arrays, using indexOf() to test if the substring is found. If it is, update activities. Commented Sep 28, 2017 at 20:59
  • @Barmar If its a substring that matches with "Arun_manohar" like "har", then I update the value of driving and jogging to be true Commented Sep 28, 2017 at 21:00

3 Answers 3

2

If you're using lodash, you can use _.intersectionWith, using a comparator function that uses indexOf().

matched_combos = _.intersectionWith(combinations, names, (combo, name) => name.indexOf(combo) != -1);

Then you can loop through matched_combos to add appropriate things to activities:

matched_combos.forEach(str => {
 switch(str) {
 "har":
 activities.driving = true;
 activities.jogging = true;
 break;
 ...
 }
});
answered Sep 28, 2017 at 21:09

Comments

1
var names = ["Arun_Manohar"];
var combos = ["abc", "def", "ghi", "har", "lmn"];
var activities = {};
for (var i = 0; i < names.length; i++) {
 for (var j = 0; j < combos.length; j++) {
 if (names[i].indexOf(combos[j]) >= 0) {
 activities.driving = true; //or whatever you need here;
 }
 }
}

Yo can also extend this code with additional checks and continue/break statements to avoid additional unnecessary loops if corresponding substring was already found.

answered Sep 28, 2017 at 21:04

8 Comments

Hey Artem, is there a way I can give the value of an element in the combinations array for the check? Like since "har" is present I update the object. If the names array is ["abcpoc"], and since "abc" is present, I want to update the object with a different set of values.
which element do you mean?
Element in the sense an item in the array.
@a2441918 If you're looking for a specific string like "har", why do you need the combinations array?
Actually names[i] is and element from names array and combos[j] is an element from combos array. You can do any check you need with those values.
|
1

Here is ES5 solution with indexOf:

var names = ["Arun_Manohar"];
var combinations = ["abc", "def", "ghi", "har", "lmn"];
var activities = {
 driving: false,
 jogging: false,
 drinking: false
}
for (var i = 0; i < names.length; i++) {
 for (var j = 0; j < combinations.length; j++) {
 if(names[i].indexOf(combinations[j]) >= 0) {
 activities.name = names[i]
 }
 }
}
console.log(activities);

And here is ES6 solution with includes:

var names = ["Arun_Manohar"];
var combinations = ["abc", "def", "ghi", "har", "lmn"];
var activities = {
 driving: false,
 jogging: false,
 drinking: false
}
for (var i = 0; i < names.length; i++) {
 for (var j = 0; j < combinations.length; j++) {
 if(names[i].includes(combinations[j])) {
 activities.name = names[i]
 }
 }
}
console.log(activities);

And here is more elegant ES6 solution with includes, forEach iterating method and arrow functions:

var names = ["Arun_Manohar"];
var combinations = ["abc", "def", "ghi", "har", "lmn"];
var activities = {
 driving: false,
 jogging: false,
 drinking: false
}
names.forEach(item1 => {
 combinations.forEach(item2 => {
 if(item1.includes(item2)) {
 activities.name = item1;
 }
 });
});
console.log(activities);

answered Sep 28, 2017 at 21:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.