ScriptyChris
ScriptyChris

Reputation: 669

Comparison of array elements and objects properties return false

I want to output which array elements are equal to objects properties.

Array and Object are like that:

var groupProperties = {
    "processors": ["producent: ","model-line: ","model: ","freq: ","cores: ","threads: ","quantity: ","netto_price: "],

    "graphics": ["producent: ","model_line: ","model: ","vram: ","directx: ","quantity: ","netto_price: "],

    "motherboards": ["producent: ","model_line: ","model: ","chipset: ","quantity: ","netto_price: "]
};

var groups = [
    PROCESSORS
    ,AMD|ATHLON|7750|2.6|6|2|7|250.00
    INTEL|i7|720QM|2.8|4|8|5|450.25
    INTEL|i5|4570|3.2|4|4|4|345.50
    ,GRAPHICS
    ,AMD|RADEON|7790|1GB|11|3|300.90
    NVIDIA|GEFORCE|8800GTS|512MB|10|1|150.99
    ,MOTHERBOARDS
    ,MSI|GAMING|GD-65|Z87|5|510.30
    ASUS|RAMPAGE|MAXIMUS-VII|Z87|6|890.99
    ASUS|UNKNOWN|P5KC|P35|1|50.00
];

My function, which doesn't work (if statement returns false, don't know why):

for (var index in groups) {
    for(var property in groupProperties) {
        console.log('PROPS '+property);
        console.log("second loop \n");
        console.log("ARRAY "+groups[index]);
        if(groups[index] === property.toUpperCase()) { 
             console.log("third loop \n");
             console.log("Array elements equal with object properties "+groups[index]);
        }
    }            
}

Output should be: 'PROCESSORS', 'GRAPHICS', 'MOTHERBOARDS'.

-------------------------------------------------------------------------------------

@edit I discovered that's something is wrong with groups array, cause in console it looks like this: https://i.sstatic.net/rbX2t.jpg

So i give whole code with manipulating on all origin array. I hope somebody helps with fixing it up.:

// var content <= there is text loaded from '.txt' file

            var splittedRows= content.split('\n');
            console.log("Splitted: \n"+ splittedRows);

            var groups = [];
            var inner = 0;

            for (index in splittedRows)
            {
                if (splittedRows[index].indexOf('GRP') == 0)
                {
                    groups[index] = splittedRows[index].slice('PRD|'.length);
                    inner = Number(index) + 1;
                        console.log('inner '+inner);
                    continue;
                }
                if (groups[inner] === undefined)
                {
                    groups[inner] = splittedRows[index].slice('PRD|'.length);
                    continue;
                }
                groups[inner] += splittedRows[index].slice('PRD|'.length);
            }
            console.log(groups);

            for(i=0; i<groups.length; i++)
            {

                if (!groups[i])
                {
                    groups.splice(i,1);
                    i--;
                }
                    //console.log("Moved "+groups[i]);
            }

Also source from where text is loaded up:

GRP|PROCESSORS PRD|AMD|ATHLON|7750|2.6|6|2|7|250.00 PRD|INTEL|i7|720QM|2.8|4|8|5|450.25 PRD|INTEL|i5|4570|3.2|4|4|4|345.50 GRP|GRAPHICS PRD|AMD|RADEON|7790|1GB|11|3|300.90 PRD|NVIDIA|GEFORCE|8800GTS|512MB|10|1|150.99 GRP|MOTHERBOARDS PRD|MSI|GAMING|GD-65|Z87|5|510.30 PRD|ASUS|RAMPAGE|MAXIMUS-VII|Z87|6|890.99 PRD|ASUS|UNKNOWN|P5KC|P35|1|50.00

@edit2

I fixed groups array by splice'ing undefined elements and slice'ing '\r' chars out from array elements. Now all works - i can compare array elements with properties of object.

Upvotes: 0

Views: 42

Answers (2)

MarkPlewis
MarkPlewis

Reputation: 2410

You need to wrap those array entries in quotes, like this:

var groups = [
  "PROCESSORS",
  "AMD|ATHLON|7750|2.6|6|2|7|250.00",
  "INTEL|i7|720QM|2.8|4|8|5|450.25",
  "INTEL|i5|4570|3.2|4|4|4|345.50",
  "GRAPHICS",
  "AMD|RADEON|7790|1GB|11|3|300.90",
  "NVIDIA|GEFORCE|8800GTS|512MB|10|1|150.99",
  "MOTHERBOARDS",
  "MSI|GAMING|GD-65|Z87|5|510.30",
  "ASUS|RAMPAGE|MAXIMUS-VII|Z87|6|890.99",
  "ASUS|UNKNOWN|P5KC|P35|1|50.00"
];

Upvotes: 0

SimpleJ
SimpleJ

Reputation: 14768

You can easily acomplish this using Array.prototype.filter and Object.prototype.hasOwnProperty:

var groupProperties = {
    "processors": ["producent: ","model-line: ","model: ","freq: ","cores: ","threads: ","quantity: ","netto_price: "],

    "graphics": ["producent: ","model_line: ","model: ","vram: ","directx: ","quantity: ","netto_price: "],

    "motherboards": ["producent: ","model_line: ","model: ","chipset: ","quantity: ","netto_price: "]
};

var groups = [
    "PROCESSORS",
    "AMD|ATHLON|7750|2.6|6|2|7|250.00",
    "INTEL|i7|720QM|2.8|4|8|5|450.25",
    "INTEL|i5|4570|3.2|4|4|4|345.50",
    "GRAPHICS",
    "AMD|RADEON|7790|1GB|11|3|300.90",
    "NVIDIA|GEFORCE|8800GTS|512MB|10|1|150.99",
    "MOTHERBOARDS",
    "MSI|GAMING|GD-65|Z87|5|510.30",
    "ASUS|RAMPAGE|MAXIMUS-VII|Z87|6|890.99",
    "ASUS|UNKNOWN|P5KC|P35|1|50.00"
];

var propGroups = groups.filter(function(group) {
    return groupProperties.hasOwnProperty(group.toLowerCase());
});

console.log(propGroups);

Upvotes: 1

Related Questions