2

i have two array object

 var data=[{m:1},{m:2},{m:3},{m:4},{m:5},{m:6},{m:7},{m:8},{m:9},{m:10},{m:11},{m:12}];
 var file=[{m:1},{m:3}];

i want to get array like [true,false,false,true,false,false,false,false,false,false,false,false]

Note : in this ayyar it will return true if it match with file array object from data array object.

i try indexOf but i cann't work for my result.

1
  • 3
    Your expected result is wrong, it should be: [true,false,true,false,false,false,false,false,false,false,false,false] Commented Apr 14, 2017 at 9:11

5 Answers 5

5

Using Array.prototype.map() and Array.prototype.some():

 let data = [{m:1},{m:2},{m:3},{m:4},{m:5},{m:6},{m:7},{m:8},{m:9},{m:10},{m:11},{m:12}];
 let file = [{m:1},{m:3}];
 
 let result = data.map(d => file.some(f => f.m === d.m));
 
 console.log(result);

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

Comments

4

You could use a hash table and map the occurence.

var data = [{ m: 1 }, { m: 2 }, { m: 3 }, { m: 4 }, { m: 5 }, { m: 6 }, { m: 7 }, { m: 8 }, { m: 9 }, { m: 10 }, { m: 11 }, { m: 12 }],
    file = [{ m: 1 }, { m: 3 }],
    hash = Object.create(null),
    result;
    
file.forEach(function (a) {
    hash[a.m] = true;
});

result = data.map(function (a) {
    return hash[a.m] || false;
});

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

ES6 with Set

var data = [{ m: 1 }, { m: 2 }, { m: 3 }, { m: 4 }, { m: 5 }, { m: 6 }, { m: 7 }, { m: 8 }, { m: 9 }, { m: 10 }, { m: 11 }, { m: 12 }],
    file = [{ m: 1 }, { m: 3 }],
    mySet = new Set(file.map(a => a.m)),
    result = data.map(a => mySet.has(a.m));

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

1 Comment

The lookup table/set is bound to be faster than my approach, especially as the list grows longer +1.
1

Try this Demo

var data=[{m:1},{m:2},{m:3},{m:4},{m:5},{m:6},{m:7},{m:8},{m:9},{m:10},{m:11},{m:12}];
 var file=[{m:1},{m:3}];
 var re = [];
 for(var i=0;i<data.length;i++){
 	let r = file.find(function findObj(o) { 
    return o.m === data[i].m;
	})
if(r){
  	re.push(true)
  }else{
  re.push(false)
  }
 }
 console.log(re)

1 Comment

I am not the downvoter, but there is only one true in your answer which is not matched with expected output of OP.
0
var data = [{m:1},{m:2},{m:3},{m:4},{m:5},{m:6},{m:7},{m:8},{m:9},{m:10},{m:11},{m:12}];
var file = [{m:1},{m:3}];

var result = [];

function remove_duplicates(data, file) {
    for (var i = 0, len = data.length; i < len; i++) { 
        for (var j = 0, len2 = file.length; j < len2; j++) { 
            if (data[i].m === file[j].m) {
                result.push("true")
            }else{
            result.push("false")
            }
        }
    }
console.log(result)
}

remove_duplicates(data,file);

Comments

0

Funny how ridiculously small it becomes with ES6.

Here, file.map(val => val.m) would give me an array of numbers. [1, 3] which I use to find whether it includes current object's m prop.

 var data=[{m:1},{m:2},{m:3},{m:4},{m:5},{m:6},{m:7},{m:8},{m:9},{m:10},{m:11},{m:12}];
 var file=[{m:1},{m:3}];
 var selection = file.map(val => val.m)
 
 var arr = data.map(obj => selection.includes(obj.m))
 
 console.log(arr)

4 Comments

for each iteration of data, you generate a new array for look up.
@NinaScholz maybe I should create it first and then use it inside?
or a set, like in my answer.
@NinaScholz okay, got it, +1, but how would one learn/know which one would be faster approach? how do you go for it?

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.