0

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 ?

2
  • What's the expected result for matchingArray and noMatchingArray? Commented Oct 12, 2017 at 13:16
  • 1
    else { noMatchingArray.push(objArray[j]); }, as well as matchingArray.push(objArray[j]); in preceding line. Commented Oct 12, 2017 at 13:18

4 Answers 4

6

This should works fine

objArray.forEach(item => arr.indexOf(item.phone) >=0 ? matchingArray.push(item) : noMatchingArray.push(item))
Sign up to request clarification or add additional context in comments.

4 Comments

Why are you using map? bad choice
@epascarello Yeah, you're right. Changed to forEach
Array.prototype.includes won't work in IE. Might be better to just stick with indexOf.
Thanks @AlexK for your suggestion. changed from Array.prototype.includes to indexOf
0

You 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

-1
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

-1

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]);
   }
}

1 Comment

See accepted answer. One liner approach vs tons of lines.

Your Answer

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.