I have an array
var arr = ['0333', '0444', '0334'];
And i have an array of objects.
var objArray = [{'name':'abc', 'phone':'0333'},
{'name':'xyz', 'phone':'0334'},
{'name':'fgfh', 'phone':'0999'},
{'name':'abc', 'phone':'0666'},
{'name':'abc', 'phone':'0444'}
]
Now i want to make a search for all arr values/indexes in objArray and separate objects with matching values, and separate with no matching values
var matchingArray = [];
var noMatchingArray = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < objArray.length; j++) {
if(objArray[j]['phone'] == arr[i]){
matchingArray.push(objArray);
}
}
}
How do i add no matching objects to noMatchingArray ?
4 Answers 4
This should works fine
objArray.forEach(item => arr.indexOf(item.phone) >=0 ? matchingArray.push(item) : noMatchingArray.push(item))
4 Comments
forEachArray.prototype.includes won't work in IE. Might be better to just stick with indexOf.Array.prototype.includes to indexOfYou can use Array.prototype.map() MDN with normal function or arrow function.
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
Noraml Map without ES6
var arr = ['0333', '0444', '0334'],
objArray = [{
'name': 'abc',
'phone': '0333'
},{
'name': 'xyz',
'phone': '0334'
},{
'name': 'fgfh',
'phone': '0999'
},{
'name': 'abc',
'phone': '0666'
},{
'name': 'abc',
'phone': '0444'
}
];
var rec='',res = arr.map(function(val){
rec = objArray.find(function(obj){return obj.phone == val});
if (rec) {
return rec;
}
});
console.log(res);
In ES6
const arr = ['0333', '0444', '0334'],
objArray = [{
'name': 'abc',
'phone': '0333'
},{
'name': 'xyz',
'phone': '0334'
},{
'name': 'fgfh',
'phone': '0999'
},{
'name': 'abc',
'phone': '0666'
},{
'name': 'abc',
'phone': '0444'
}
];
let res = arr.map(val => {
let rec = objArray.find(obj => obj.phone == val);
if (rec) {
return rec;
}
});
console.log(res);
Comments
function SearchArray() {
var arr = ['0333', '0444', '0334'];
var objArray = [{ 'name': 'abc', 'phone': '0333' },
{ 'name': 'fgfh', 'phone': '0999' },
{ 'name': 'abc', 'phone': '0666' },
{ 'name': 'abc', 'phone': '0444' }
];
var matchingArray = [];
var noMatchingArray = [];
for (var i = 0; i < arr.length; i++) {
var isFound = false;
for (var j = 0; j < objArray.length; j++) {
if (objArray[j]['phone'] == arr[i]) {
matchingArray.push(objArray[j]);
isFound = true;
break;
}
}
if (!isFound)
{
noMatchingArray.push(arr[i]);
}
}
}
Comments
First of all, modify your existing loops and add a break statement. If I do not misunderstand the purpose of your match algorithm, you want to stop looking for a match for this phone number, if you have already matched.
Moreover, I think you have an error in your code. You do not want to push the objArray in the matchingArray, right? Just the matchedObject -> j
Answering your question. I would work with a second loop looking over all objects in the objArray, pushing those who are not in the array matchingArray to the noMatchingArray.
var matchingArray = [];
var noMatchingArray = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < objArray.length; j++) {
if(objArray[j]['phone'] == arr[i]){
matchingArray.push(objArray[j]);
break;
}
}
}
for (var j = 0; j < objArray.length; j++) {
var marked = false;
for (var i = 0; i < matchingArray.length; i++) {
if (objArray[j] === matchingArray[i]) {
marked = true;
break;
}
}
if(!marked){
noMatchingArray.push(objArray[j]);
}
}
even better: combining both loops back together while switching inner and outer look of your initial algorithm makes it even more readable and faster than my first solution.
var matchingArray = [];
var noMatchingArray = [];
for (var j = 0; j < objArray.length; j++) {
var marked = false;
for (var i = 0; i < arr.length; i++) {
if(objArray[j]['phone'] == arr[i]){
matchingArray.push(objArray[j]);
marked = true;
break;
}
}
if(!marked){
noMatchingArray.push(objArray[j]);
}
}
matchingArrayandnoMatchingArray?else { noMatchingArray.push(objArray[j]); }, as well asmatchingArray.push(objArray[j]);in preceding line.