var a = new Array();
a.push({"1": "apple", "2": "banana"});
a.push({"3": "coconut", "4": "mango"});
GetIndexByValue(a, "coconut");
function GetIndexByValue(arrayName, value) {
var keyName = "";
var index = -1;
for (var i = 0; i < arrayName.length; i++) {
var obj = arrayName[i];
for (var key in obj) {
if (obj[key] == value) {
keyName = key;
index = i;
}
}
}
//console.log(index);
return index;
}
// Filename: gulpfile.js
var gulp = require('gulp');
var invert = require('lodash.invert');
gulp.task('test-invert', function () {
var hash = {
foo: 1,
bar: 2
};
var val = 1;
var key = (invert(hash))[val]; // << Here's where we call invert!
console.log('key for val(' + val + '):', key);
});
2)安装lodash。倒转包装,大口吞咽
$ npm i --save lodash.invert && npm install gulp
3)测试它是否有效:
$ gulp test-invert
[17:17:23] Using gulpfile ~/dev/npm/lodash-invert/gulpfile.js
[17:17:23] Starting 'test-invert'...
key for val(1): foo
[17:17:23] Finished 'test-invert' after 511 μs
var keyByValue = function(value) {
var kArray = Object.keys(greetings); // Creating array of keys
var vArray = Object.values(greetings); // Creating array of values
var vIndex = vArray.indexOf(value); // Finding value index
return kArray[vIndex]; // Returning key by value index
}
对象的键和值:
var greetings = {
english : "hello",
ukranian : "привіт"
};
let samplLst = [{id:1,title:Lorem},{id:2,title:Ipsum}]
let sampleKey = _.findLastIndex(samplLst,{_id:2});
//result would be 1
console.log(samplLst[sampleKey])
//output - {id:2,title:Ipsum}
// Oject that contains different icons for different extentions
const icons = {
"music": ["mp3", "m4a", "ogg", "acc", "flac","m3u", "wav"],
"video": ["mp4","webm", "mkv", "avi", "mov", "m4v", "mpeg"],
"image": ["jpg", "gif", "png", "jpeg", "tif", "psd", "raw", "ico"],
"archives": ["zip", "rar", "tar", "dmg", "jar"],
"3d-files": ["3ds", "dwg", "obj", "dae", "skp", "fbx"],
"text": ["doc", "rtf", "txt", "odt", "tex"],
"vector-graphics":["ai", "svg"],
"pdf": ["pdf"],
"data": ["xml", "csv", "xls"]
}
const get_icon_Key =( icons_object,file_extention) => {
// For each key we chack if the value is contained in the list of values
let key = Object.keys(icons_object).find(
k=> icons[k].find(
// At this leve we check if the extention exist in the array of the specific object value ie. 'music', 'video' ...
icons_ext => icons_ext === file_extention)
// if we find it means this is the key we are looking for
? true: false);
return key
}
console.log(`The icon of for mp3 extention is: => ${get_icon_Key(icons,"mp3")}`)
console.log(`The icon of for mp4 extention is: => ${get_icon_Key(icons,"mp4")}`)
let object = { nine_eleven_was_a_inside_job: false, javascript_isnt_useful: false }
// Complex, dirty but useful. Handle mutiple matchs which is the main difficulty.
Object.prototype.getKeyByValue = function (val) {
let array = [];
let array2 = [];
// Get all the key in the object.
for(const [key] of Object.entries(this)) {
if (this[key] == val) {
// Putting them in the 1st array.
array.push(key)
}
}
// List all the value of the 1st array.
for(key of array) {
// "If one of the key in the array is equal to the value passed in the function (val), it means that 'val' correspond to it."
if(this[key] == val) {
// Push all the matchs.
array2.push(key);
}
}
// Check the lenght of the array.
if (array2.length < 2) {
// If it's under 2, only return the single value but not in the array.
return array2[0];
} else {
// If it's above or equal to 2, return the entire array.
return array2;
}
}
/*
Basic way to do it wich doesn't handle multiple matchs.
let getKeyByValue = function (object, val) {
for(const [key, content] of Object.entries(object)) {
if (object[key] === val) {
return key
}
}
}
*/
console.log(object.getKeyByValue(false))