1

Ok, so I have an array of objects. Which get defined with a randomly generated table value. I want to search the array of objects and execute code if one table value eg. "table1" occurs more than 6 times.

Here is the array of objects I have:

  var data = [{
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
                {
                    name: "",
                    pref: "",
                    table: ""
                },
            ];
5
  • 4
    the objects have all the same value, actually ... do you have a question? Commented Mar 2, 2017 at 21:31
  • You can use a loop to iterate your array. Commented Mar 2, 2017 at 21:32
  • what did you try? Commented Mar 2, 2017 at 21:33
  • Just run a filter on the array, data = data.filter ( x => { return x.name === 'value_I_want' } ). When it's done, if that array has a length >= 6, run your code. Alternatively if you don't want to allocate the new array, iterate the array, look for the property, increment a counter if you find it, when that counter is === 6, break out of the loop and run your code. Commented Mar 2, 2017 at 21:35
  • @NinaScholz the values are all defined later in the code. Commented Mar 2, 2017 at 21:43

3 Answers 3

2

You can use this example:

if (data.filter((item)=>{return item.table === "1"}).length >= 6)
Sign up to request clarification or add additional context in comments.

3 Comments

Great! Now can I use that same condition in a while loop?
no, you should not use filter for counting, it is the wrong method, because you generate a result array and refuse it later.
@NinaScholz Oh, so if I use your method how can I use a while loop to run until the amount of table 1 values are less than 6?
1

You could use Array#reduce for counting with condition.

ES6

if (data.reduce((sum, item) => sum + (item.table === "1"), 0) > 6) {
    // do something
}

ES5

if (data.reduce(function (sum, item) { return sum + (item.table === "1"); }, 0) > 6) {
    // do something
}

... if I use your method how can I use a while loop to run until the amount of table 1 values are less than 6?

You could use a while loop with a closure over the value to check for.

function countTable(value) {
    return function (sum, item) {
        return sum + (item.table === value);
    };
}


while (data.reduce(countTable('table1'), 0) >= 6) {
    // do something
}

6 Comments

Ok, so I need this to change the table value until there are 6 or less 'table1' values. I have the while loop now how do I change the table value? Is there any way to do it similar to how you would do it in a for loop? Ex. data[i].table = "table2";
@jscoder001, please accept an answer before extending the question in the comments. i see actually no reason why and what property to change. you might as a new question with extended parameters.
@NinaScholz Sorry, I'm new to SO, but I'm not as much asking you to change your code. I just need some guidance as to what to put inside the while loop. Should I ask this as another question?
it is more a question, what you wan. in the question, you ask for a occurrence of more than 6 times, now you need 6 or less. maybe you illustrate with an example what you need in a new question.
Well, I'm checking for 6 or more and changing it until there are 6 or less. I'll ask another question though unless you think you have a quick solution.
|
0

You can also use Array.prototype.some to stop counting if sufficient matching elements have been found.

function multiFind( array, property, value , count) {
    return array.some( function( c) {
        count -= c[property] == value;
        return count<=0;
    });
}

Alternatively this could be compressed and written in ES6 syntax as

var multiFind =  (a, prop, v, n) => a.some( c=> (n-=c[prop] == v)<=0);

Note both versions use automatic type conversion to convert boolean true to 1 and false to 0 for counting purposes. A call to the function can then be used in a conditional statement such as.

while( multiFind( data, "table", "table1", 6) ) {
    // do stuff while there are more than 5 "table1" table properties in data array
}

1 Comment

Ok, I almost have it working. Any chance you could tell me how I could define a variable which would allow me to manipulate any value that is "table1"? I need to move the value to another table because each table may only have 6 occupants.

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.