2

I'm tryign to write code that will loop through an array "productsArray" and match it against my productPropertyArray to pull matching information.

however productsArray is an array in an array that contains an object with the data. My Question is how can I loop through both arrays and then return the matching data.

Current function:

    var pList = productsArray
    if (productPropertyArray.length === 0 || productsArray.length === 0) return [];
    for (var i = 0; i < pList.length; i++) {
        for (var j = 0; j < pList[i].length; j++) {
            if (pList[i][j] === productPropertyArray) {
               return productPropertyArray;
            } else {
                continue;
            }
        }

    }
    return [];
    };

example of pList:

productsArray = [
            [{"sku" : "131674"},
            {"sku" : "84172"}],
            [{"productID" : "1234"}
            ,{"productID" : "12345"}],
            [{"test": 1},{"test": 1}],
            [{"test": 1},{"sellAlone": false,"test": 1}],
            [{"test": 1}],
            [{"sellAlone": false,"test": 1}]
        ];

example of productPropertyArray: (its an argument thats replaced by the following)

productSKUArray = [
            "00544MF24F575", 
            "131674", 
            "84172"
        ];

productPropertyArray is just an argument in the function which is replaced by productSKUArray The setup goes like this: function(productProperty, productPropertyArray, productsArray) {

productProperty is just a string that contains sku or productID any ideas are appreciated. thanks.

8
  • can you give pList example and ProductPropertyArray? Commented Oct 3, 2014 at 19:07
  • Sorry about that! Examples added Commented Oct 3, 2014 at 19:10
  • what about ProductPropertyArray? Commented Oct 3, 2014 at 19:16
  • that bottom example is an example of an array that goes into the function. Commented Oct 3, 2014 at 19:18
  • So your match will be something from ProductPropertyArray that looks exacly like productSKUArray right? Commented Oct 3, 2014 at 19:18

3 Answers 3

1

Check this out:

http://jsfiddle.net/v9d7bjms/2/

function find() {
    var productsArray = [
            [{"sku" : "131674"},
            {"sku" : "84172"}],
            [{"productID" : "1234"}
            ,{"productID" : "12345"}],
            [{"test": 1},{"test": 1}],
            [{"test": 1},{"sellAlone": false,"test": 1}],
            [{"test": "00544MF24F575"}],
            [{"sellAlone": false,"test": 1}]
        ],
        pList = productsArray,
        productSKUArray = [
            "00544MF24F575", 
            "131674", 
            "84172"
        ];

    // All arrays matching your productsSKUArray
    var findings = productsArray.filter(function (productProperty) {
        // .some returns true after finding first matching element (and breaks the loop)
        return productProperty.some(function (obj) { 
            var keys = Object.keys(obj);
            // We need to get all the "values" from object so we interate over
            // the keys and check if any value matches something from productSKUArray
            return keys.some(function (key) { 
                // Check if value exists in productsSKUArray
                return productSKUArray.indexOf(obj[key]) > -1;
            });
        });
    });

    return findings;
}

console.log(find());

.filter will return all arrays containing objects with values from productSKUArray.

See Array.prototype.filter, Array.prototype.some and Array.prototype.indexOf for method reference.

Sign up to request clarification or add additional context in comments.

4 Comments

Cool. Can you explain little bit of your code. I mean what you are doing for future reading cases. :) Up voted for sure.
@Phoenix done. @TimeDead This is probably not the most efficient way of doing this but it's readable and makes nice usage of "functional JS". For polishing you could take the deepest functions out of find() (like the last one with .indexOf) and thus save some memory and CPU time on redefining functions.
@veritas I think OP should change his data structure in order to reduce computation time. Currently it is O(n3)
the current data arrays are just unit test mock data. Thanks for the advise.
0

The inner if needs to refer to pList[i][j].

Comments

0

This will output [{sku: "131674"}, {sku: "84172"}].

var matchingData = [];
        for(var productProperties in productsArray){

            var pp = productsArray[productProperties];
            for(var property in pp) {

                var p = pp[property];
                for(var propertyName in p){

                    var propertyValue = p[propertyName];

                    for(var i in productSKUArray){

                        if(propertyValue == productSKUArray[i]){
                            matchingData.push(p);
                            break;
                        }

                    }

                }

            }
        }

but this is just the brute force solution.

Comments

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.