1

If any of the choice object from choices array is not found in any of the results array hide choice element in this case {"choice": "f"} and {"choice": "g"} is not available in any of those 3 results so I need to hide it. How can I do it?

{"choices":[
    {"choice": "a"},
    {"choice": "b"},
    {"choice": "c"},
    {"choice": "d"},
    {"choice": "e"},
    {"choice": "f"},
    {"choice": "g"}
],
"results":[
    {"result":["a", "b", "c"]},             
    {"result":["a", "b", "d"]},
    {"result":["a", "b", "e"]},
2
  • 1
    what means hide? do you need an array with the result? Commented Mar 7, 2017 at 15:13
  • the question is unclear, the posted code is just a cut chunk of some code with no beginning and ending(voted to close) Commented Mar 7, 2017 at 15:17

4 Answers 4

2

You could collect all values from result and filter choices.

var object = { choices: [{ choice: "a" }, { choice: "b" }, { choice: "c" }, { choice: "d" }, { choice: "e" }, { choice: "f" }, { choice: "g" }], results: [{ result: ["a", "b", "c"] }, { result: ["a", "b", "d"] }, { result: ["a", "b", "e"] }] },
    hash = Object.create(null),
    notIn = [];

object.results.forEach(function (a) {
    a.result.forEach(function (b) {
        hash[b] = true;
    });
});

notIn = object.choices.filter(function (a) {
    return !hash[a.choice];
});

console.log(notIn);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES6 with Set

var object = { choices: [{ choice: "a" }, { choice: "b" }, { choice: "c" }, { choice: "d" }, { choice: "e" }, { choice: "f" }, { choice: "g" }], results: [{ result: ["a", "b", "c"] }, { result: ["a", "b", "d"] }, { result: ["a", "b", "e"] }] },
    results = object.results.reduce((s, a) => new Set([...s, ...a.result]), new Set),
    notIn = object.choices.filter(a => !results.has(a.choice));

console.log(notIn);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

+1 for the Set solution, maybe opting for let instead of var would be better for a full ES6 solution?
@Anis, actually the scope is not the problem.
I completely agree, and since it doesn't matter I just thought that let would be a better default in this case.
0

I'm not sure whether you are asking for generic implementation ideas, or pointers as to how to implement it in javascript. I'll go for the general idea. You can simply use a set to store all the possible results, then iterate through the choices and check for each choice whether it is inside the set. If not, you can take the action you want like hiding it, whatever that means.

Comments

0

You can use filter() on data.choices and then some() and includes() to check if choice exists in some of the objects in results array.

var data = {"choices":[{"choice":"a"},{"choice":"b"},{"choice":"c"},{"choice":"d"},{"choice":"e"},{"choice":"f"},{"choice":"g"}],"results":[{"result":["a","b","c"]},{"result":["a","b","d"]},{"result":["a","b","e"]}]}

data.choices = data.choices.filter(function(o, i) {
  var check = data.results.some(e => e.result.includes(o.choice))
  return check;
})

console.log(data)

Comments

0

var choices = {choices:[{choice:"a"},{choice:"b"},{choice:"c"},{choice:"d"},{choice:"e"},{choice:"f"},{choice:"g"}],results:[{result:["a","b","c"]},{result:["a","b","d"]},{result:["a","b","e"]}]},

    elems1 = choices.results.map(v => v.result).reduce((a,b) => a.concat(b)),
    filtered = [...new Set(elems1)];
    choices.choices = choices.choices.filter(v => filtered.some(c => c == v.choice) );
    console.log(choices);

5 Comments

Please add some explanation of why/how this code helps the OP. This will help provide an answer future viewers can learn from. See this Meta question and its answers for more information. This seems to be becoming a theme here, @Kinduser...
@MikeMcCaughan Actually I don't want to write anything because this is over complicated... I will wait for Nina response and I will decide to leave or to delete my answer. If I will decide to leave it, I will describe each step.
@Kinduser, you could reduce with Set, instead of mapping the values.
@NinaScholz Can you show me an example? Just a short one.
@Kinduser, please see my es6 example.

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.