简体   繁体   中英

How to specify two numbers in multidimensional array and return 'id' value

This is my array (I think I have written it correct):

var data = {
  0 : {'id':754,'word':'hello'},
  1 : {'id':324,'word':'bye'},
  2 : {'id':111,'word':'nice'}
}

I'd like to specify one or two 'id's, eg var nums = [324, 111];

and have the script return what 'id' was not in nums , and therefore the output would be 754

How can I do this?

PS. I wasnt sure how to title this

Try this..

 var data = { 0 : {'id':754,'word':'hello'}, 1 : {'id':324,'word':'bye'}, 2 : {'id':111,'word':'nice'} }; var nums = [324, 111]; //function for get the values function getValues(data, nums){ var output = []; for (var i in data){ if (nums.indexOf(data[i].id)==-1) output.push(data[i].id); } return output; } document.write(getValues(data,nums)); //calling the function 

Arrays like that should be declared like so:

var data = [{'id':754,'word':'hello'},
        {'id':324,'word':'bye'},
        {'id':111,'word':'nice'}];

That way you can use the new Array prototype functions like filter and forEach .

var excludedIds = [324, 111];

var objectsWithoutExcluded = data.filter(function(item){
  return excludedIds.indexOf(item.id) > 0;
});

And there you go.

You can do this entirely using lodash (which is basically underscore).

 var data = { 0 : {'id':754,'word':'hello'}, 1 : {'id':324,'word':'bye'}, 2 : {'id':111,'word':'nice'} }; function getValues (data, excludedIDs) { var allIDs = _.pluck(data, 'id'); return _.difference(allIDs, excludedIDs); } document.write(getValues(data,[324,111])); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script> 

  1. Convert data to an array using Object.keys .
  2. filter the output.
  3. map the filtered results to the ids.

Code:

 function missing(nums) { return Object.keys(data) .filter(function(n) { return nums.indexOf(data[n].id) === -1; }) .map(function(n) { return data[n].id; }); } //missing var data = { 0 : {'id':754,'word':'hello'}, 1 : {'id':324,'word':'bye'}, 2 : {'id':111,'word':'nice'} } alert(missing([324, 111])); //754 alert(missing([])); //754, 324, 111 alert(missing([111, 754])); //324 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

If your data were an array instead, this would work:

data.filter(function(datum) {
    return nums.indexOf(datum.id) < 0;
}).map(function(datum) {
    return datum.id;
}); 
//=> [754]

With your current (somewhat odd, I might add) data structure, you might use this:

Object.keys(data).filter(function(key) {
    return nums.indexOf(data[key].id) < 0;
}).map(function(key) {
    return data[key].id;
});

I library like Ramda might make this more concise:

R.map(R.prop('id'), R.reject(R.pipe(R.prop('id'), R.flip(R.contains)(nums)), data));
//=> [754]

(this is for the array case; I haven't thought through the other one.)

Nest your Array like this.

var data = {
  0 : {'id': {value1: 324, value2: 111},'word':'hello'},   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM