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.
3 Answers 3
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;
...
}
});
Comments
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.
8 Comments
"har"
, why do you need the combinations
array?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.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);
indexOf()
to test if the substring is found. If it is, updateactivities
.