Check if second array has any string from first array - javascript

I am trying to find if second array has any string from the first one.
Not sure what I am doing wrong
const domainAlertList = ["#domain1", "#tomato2", "#carrot3"];
const search = ["test#domain1.com", "test#tomato2.com"];
const myFunc = () => {
return search.some((r) => domainAlertList.includes(r));
};
console.log(myFunc());
It returns false instead of true

const domainAlertList = ["#domain1", "#tomato2", "#carrot3"];
const search = ["test#domain1.com", "test#tomato2.com"];
const myFunc = () => {
return domainAlertList.some((r) => {
return search.some(t => t.includes(r))
});
};
console.log(myFunc());

There are many ways to approach this. One of them could be the indexOf() method.
let str = "test#domain1.com";
str.indexOf('domain1') !== -1 // true
source

You have to map through the second array too like this
const domainAlertList = ["#domain1", "#tomato2", "#carrot3"];
const search = ["test#domain1.com", "test#tomato2.com"];
const myFunc = () => {
return search.some((r) => domainAlertList.some(domain=>{domain.includes(r)});
};
console.log(myFunc());

You can iterate and use array filter function.
const domainAlertList = ["#domain1", "#tomato2", "#carrot3"];
const search = ["test#domain1.com", "test#tomato2.com"];
let r = []
domainAlertList.forEach(i => {
r.push( search.filter(i1 => i1.includes(i)) )
})
newArr = r.map((a) => {
if(a.length != 0) {
return a
}
}).filter((a) => a);
console.log(newArr)

const domainAlertList = ["#domain1", "#domain1", "#carrot3"];
const search = ["test#domain1.com", "test#domain1.com"];
const myFunc = () => {
return domainAlertList.some((domain) =>
search.some((email) => email.includes(domain))
);
};
console.log(myFunc());

Related

how to convert string array of objects to array of objects

I have an array that I want to convert but I have no idea how to do it
how can i convert this array
const a = ['{/run:true}', '{/sleep:false}'];
in this array
const b = [{'/run':true}, {'/sleep':false}];
Using map and a regular expression:
const a = ['{/run:true}', '{/sleep:false}'];
const b = a.map(s => {
const [_,k,v] = s.match(/\{(.+):(.+)}/);
return {[k]: JSON.parse(v)};
});
console.log(b);
Or other way is to run a sting replacement and JSON.parse
const a = ['{/run:true}', '{/sleep:false}'];
const b = JSON.parse("[" + a.toString().replace(/\{([^:]+)/g, '{"$1"') + "]");
console.log(b);
Created this simple function to do exactly that
const a = ['{/run:true}', '{/sleep:false}'];
// desired output
const b = [{ run: true }, { sleep: false }];
const c = a.map(item => {
const key = item.match(/\w+/)[0];
const value = item.match(/true|false/)[0];
return { [key]: value === 'true' };
});
console.log(c);
const a = ['{/run:true}', '{/sleep:false}'];
const output = a.map(item => {
const normalize = item.replaceAll("{", "").replaceAll("}", "")
const splitedStr = normalize.split(':');
const key = splitedStr[0];
const value = splitedStr[1];
return {[key]: value}
})
console.log(output)
const a = ['{/run:true}', '{/sleep:false}'];
const c = a.map(item => {
return {
['/' + item.split(':')[1].split('{')[0]]: item.split(':')[1].split('}')[0]
};
});
console.log(c);

How to wait array until complete, then execute the function?

i have some trouble.
I want my function execute when my array is complete.
But the my function execute although my array is not complete.
can you fix my code please..
const historyPermintaanObat2 = obat => {
let array = [];
db.child("itemPermintaan")
.orderByChild("id")
.equalTo(obat.id)
.on("child_added", snapshot => {
let obj = snapshot.val();
let registerPermintaanRef = db.child(
`registerPermintaan/${obj.idPermintaan}`
);
registerPermintaanRef.once("value", snap => {
let username = snap.val().username;
let createdAt = snap.val().createdAt;
let approvedAt = snap.val().approvedAt;
let unit = snap.val().unit;
let obj2 = { ...obj, username, createdAt, approvedAt, unit };
array.push(obj2);
});
});
return array;
};
The result is [] empty array.
If i change the return array.length > 0 && array nothing is happen..
I have figured it out, use once and Promise.all()
const historyPermintaanObat2 = obat => {
db.child("itemPermintaan")
.orderByChild("id")
.equalTo(obat.id)
.once("value", snapshot => {
let array = [];
let obj = Object.values(snapshot.val());
obj.forEach(e => {
let registerPermintaanRef = db.child(
`registerPermintaan/${e.idPermintaan}`
);
let promise = registerPermintaanRef.once("value").then(snap => {
let username = snap.val().username;
let createdAt = snap.val().createdAt;
let approvedAt = snap.val().approvedAt;
let unit = snap.val().unit;
let obj2 = { ...e, username, createdAt, approvedAt, unit };
return obj2;
});
array.push(promise);
});
return Promise.all(array).then(value => {
return value;
});
});
};
Here's the reference i've got Promise.all with Firebase DataSnapshot.forEach

Converting ternary operator to if

This is a trivial question but I am having hard time to convert ternary operator to if. Here is what I tried.
function memoize(fn) {
const cache = {};
return (...args) => {
const stringifiedArgs = stringifyArgs(args);
const result = (cache[stringifiedArgs] = !cache.hasOwnProperty(
stringifiedArgs
)
? fn(...args)
: cache[stringifiedArgs]);
return result;
};
}
// function memoize(fn) {
// const cache = {};
// return (...args) => {
// const stringifiedArgs = stringifyArgs(args);
// return result = (if (cache[stringifiedArgs] = !cache.hasOwnProperty(stringifiedArgs)) {
// fn(...args);
// } else {
// cache[stringifiedArgs];
// })
// };
// }
This is the cleanest I can get it--check the property and save the result of the memoized function in the cache if it doesn't exist. Then return it from the cache.
function memoize(fn) {
const cache = {};
return (...args) => {
const stringifiedArgs = stringifyArgs(args);
if (!cache.hasOwnProperty(stringifiedArgs)) {
cache[stringifiedArgs] = fn(...args);
}
return cache[stringifiedArgs];
};
}
You could also use the in operator pretty safely here:
function memoize(fn) {
const cache = {};
return (...args) => {
const stringifiedArgs = args.join(`,`); // just for testing
if (!(stringifiedArgs in cache)) {
cache[stringifiedArgs] = fn(...args);
}
return cache[stringifiedArgs];
};
}
const fib = memoize(n => n < 2 ? n : fib(n - 1) + fib(n - 2));
console.log(fib(78));
You probably need an iife if you want to maintain the same structure. However, this is not the cleanest way to do it...
function memoize(fn) {
const cache = {};
return (...args) => {
const stringifiedArgs = stringifyArgs(args);
const result = cache[stringifiedArgs] = (() => {
if (!cache.hasOwnProperty(stringifiedArgs)) {
return fn(...args);
} else {
return cache[stringifiedArgs];
}
})();
return result;
};
}

javascript (es6) return value of filter in forEach

I have function like below.
If the value of the filter is an array with more than 4 I want to return just the key.
For instance const result = gethitedShips(); // result be 1 or 2 but I get undefined
I totally got confused where to return what
getHitShips = () => {
const { players } = this.props;
return Object.keys(players).forEach(key => {
const hitShips = players[key].ships.filter(ship => ship.health <= 0);
if (hitShips.length >= 5) return key;
return null;
});
};
You could filter the keys by checking the length
const getHitedShips = () => {
const { players } = this.props;
return Object
.keys(players)
.filter(key => players[key].ships.filter(ship => ship.health <= 0).length >= 5);
};

Transform elements of an array into objects / Javascript

I have a challenge to create a simple Notes manager in JS, I've written a function that takes one string, gives it and id and pushes it to an array of notes.
let nextId = 0;
const getId = () => nextId++;
let notes = [{id: getId(), value: 'Note'}];
const addNote = (input) => {
notes.push({id:getId(), value: input});
console.log('Note added');
I now struggle with a function that will take multiple strings as parameters
('own', 'snail', 'platypus')
create an object for each element with id/value(string) and push it to the main array.
The result should look like:
[{ id: 1, value: 'owl'},
{ id: 2, value: 'snail'}]
So far I have this, it assigns ID correctly, but the loop fails
const batchAddNotes = (values) => {
let obj = {};
for (i = 0; i < values.length; i++) {
obj.id = (getId());
obj.value = (values[i]);}
return obj;};
To have your variables in a certain scope, I'd pack it all in a class (or a function). As you're using arrow functions, the class should be ok. To add multiple nodes the way you've shown; using var-args, you can create a method that expects those with (...input)
class Notes {
constructor() {
this.nextId = 0;
this.nodes = [{
id: this.getId(),
value: 'Note'
}];
}
addNote(input) {
this.nodes.push({
id: this.getId(),
value: input
})
}
getId() {
return this.nextId++;
}
addNotes(...input) {
input.forEach(e => this.addNote(e));
}
}
const notes = new Notes();
notes.addNotes('own', 'snail', 'platypus');
console.log(notes.nodes);
Use the functions arguments object. It's an array of all the arguments that are being passed to a function. Then you can loop over them and run your functionality on them each time.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
You could use the arguments passed in your function as var args
const addNote = _ => {
for(var i = 0; i < arguments.length; i++){
notes.push({id:getId(), value: arguments[i]});
console.log('Note added');
}
}
use rest params :
const myFn = (...values) => {
let tmpArr = [];
for(let i = 0 ; i < values.length ; i++){
tmpArr.push({
id : i + 1,
value : values[i]
});
}
return tmpArr;
}
const result = myFn('own', 'snail', 'platypus');
console.log(result);
This is how it look like when using Rest Params and reusing your first function. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters).
You can add so many notes as you want (addMultipleNotes can receive indefinite number of arguments )
let nextId = 0;
const getId = () => nextId++;
let notes = [{id: getId(), value: 'Note'}];
const addSingleNote = (input) => {
notes.push({id:getId(), value: input});
console.log('Note added');
};
const addMultipleNotes = (...args) => {
for(let i = 0; i < args.length; i++){
addSingleNote(args[i]);
}
};
addMultipleNotes('one', 'two', 'three');
console.log(notes);
First of all, note how I've used an IIFE and a closure to create an id generator.
In the other hand, rest parameters, Array#map and parameter spread are your friends:
const incrId = (() => {
let id = 0
return () => ++id
})()
const valuesToNotes = (...values) => values.map(value => ({
id: incrId(),
value
}))
const notes = []
// Parameter spread (i.e. '...') gives each
// array item in the output of valuesToNotes
// as if you would use Function#apply
notes.push(...valuesToNotes('a', 'b', 'c'))
console.log(notes)
Yet another more functional approach which doesn't mutate the input notes and produces a new one with existing notes plus the ones transformed from values:
const concat = xs => ys => xs.concat(ys)
const map = f => xs => xs.map(f)
const pipe = xs => x => xs.reduce((r, f) => f(r), x)
const incrId = (() => {
let id = 0
return () => ++id
})()
const valueToNote = value => ({
id: incrId(),
value
})
const notes = []
const appendNotes = pipe([map(valueToNote), concat(notes)])
const moreNotes = appendNotes(['a', 'b', 'c'])
console.log(moreNotes)

Categories