按值查找数组的自定义字符串索引 - 对于多个数组
Find custom string index of an array by value - for multiple arrays
我对数组有点问题。
我有一个字符串作为值,它对于数组索引是唯一的,例如"daa12d956752gja2":
g_nodeMapping["87686479/welcome.html"] = "daa12d956752gja2";
这个字符串是我所知道的。我需要得到的是索引,所以“87686479/welcome.html”。问题是……我有几十个这样的数组。它基本上是这样的:
g_nodeMapping = [];
g_nodeMapping["8374628/test.html"] = "489fa3682975da";
g_nodeMapping["8953628/anothersite.html"] = "gi764295hf46";
g_nodeMapping["267857543/helpplx.html"] = "8653468te87a";
...
我试过indexOf
的方法,好像找不到等号后面的值的数组索引
不幸的是,我无法更改数组。
非常感谢您的帮助。抱歉格式问题,我在手机上。
您可以通过获取 object/array 中的所有键并找到值来找到键。
function getKey(object, value) {
return Object.keys(object).find(k => object[k] === value);
}
var g_nodeMapping = [];
g_nodeMapping["8374628/test.html"] = "489fa3682975da";
g_nodeMapping["8953628/anothersite.html"] = "gi764295hf46";
g_nodeMapping["267857543/helpplx.html"] = "8653468te87a";
g_nodeMapping["87686479/welcome.html"] = "daa12d956752gja2";
console.log(getKey(g_nodeMapping, "daa12d956752gja2"));
您可以定义一个函数 findCustomKey
,它将数组元素(或值)作为参数,returns 作为键。下面的例子表明:
var arr = [];
arr["8374628/test.html"] = "489fa3682975da";
arr["8953628/anothersite.html"] = "gi764295hf46";
arr["267857543/helpplx.html"] = "8653468te87a";
function findCustomKey(ele) {
let keys = Object.keys(arr);
for (let keyEle of keys) {
if (arr[keyEle] == ele) {
return keyEle;
}
}
}
console.log(findCustomKey("489fa3682975da"));
console.log(findCustomKey("8653468te87a"));
console.log(findCustomKey("abcd123"));
输出:
8374628/test.html
267857543/helpplx.html
undefined
另一个版本(添加编辑):
这是另一种编码方式findCustomKey
(使用方式保持不变):
function findCustomKeyV2(ele) {
return Object.keys(arr).filter(k => arr[k] == ele)[0];
}
另一个版本:
添加此版本的解决方案是因为上述代码在 IE 浏览器上不起作用。以下代码适用于 Firefox、Chrome 和 IE11 浏览器。
var arr = [];
arr['8374628/test.html'] = '489fa3682975da';
arr['8953628/anothersite.html'] = 'gi764295hf46';
arr['267857543/helpplx.html'] = '8653468te87a';
var arrMap = new Map();
for (let k in arr) {
arrMap.set(arr[k], k);
}
console.log(arrMap.get('489fa3682975da'));
console.log(arrMap.get('8653468te87a'));
console.log(arrMap.get('abcd123'));
我对数组有点问题。
我有一个字符串作为值,它对于数组索引是唯一的,例如"daa12d956752gja2":
g_nodeMapping["87686479/welcome.html"] = "daa12d956752gja2";
这个字符串是我所知道的。我需要得到的是索引,所以“87686479/welcome.html”。问题是……我有几十个这样的数组。它基本上是这样的:
g_nodeMapping = [];
g_nodeMapping["8374628/test.html"] = "489fa3682975da";
g_nodeMapping["8953628/anothersite.html"] = "gi764295hf46";
g_nodeMapping["267857543/helpplx.html"] = "8653468te87a";
...
我试过indexOf
的方法,好像找不到等号后面的值的数组索引
不幸的是,我无法更改数组。
非常感谢您的帮助。抱歉格式问题,我在手机上。
您可以通过获取 object/array 中的所有键并找到值来找到键。
function getKey(object, value) {
return Object.keys(object).find(k => object[k] === value);
}
var g_nodeMapping = [];
g_nodeMapping["8374628/test.html"] = "489fa3682975da";
g_nodeMapping["8953628/anothersite.html"] = "gi764295hf46";
g_nodeMapping["267857543/helpplx.html"] = "8653468te87a";
g_nodeMapping["87686479/welcome.html"] = "daa12d956752gja2";
console.log(getKey(g_nodeMapping, "daa12d956752gja2"));
您可以定义一个函数 findCustomKey
,它将数组元素(或值)作为参数,returns 作为键。下面的例子表明:
var arr = [];
arr["8374628/test.html"] = "489fa3682975da";
arr["8953628/anothersite.html"] = "gi764295hf46";
arr["267857543/helpplx.html"] = "8653468te87a";
function findCustomKey(ele) {
let keys = Object.keys(arr);
for (let keyEle of keys) {
if (arr[keyEle] == ele) {
return keyEle;
}
}
}
console.log(findCustomKey("489fa3682975da"));
console.log(findCustomKey("8653468te87a"));
console.log(findCustomKey("abcd123"));
输出:
8374628/test.html
267857543/helpplx.html
undefined
另一个版本(添加编辑):
这是另一种编码方式findCustomKey
(使用方式保持不变):
function findCustomKeyV2(ele) {
return Object.keys(arr).filter(k => arr[k] == ele)[0];
}
另一个版本:
添加此版本的解决方案是因为上述代码在 IE 浏览器上不起作用。以下代码适用于 Firefox、Chrome 和 IE11 浏览器。
var arr = [];
arr['8374628/test.html'] = '489fa3682975da';
arr['8953628/anothersite.html'] = 'gi764295hf46';
arr['267857543/helpplx.html'] = '8653468te87a';
var arrMap = new Map();
for (let k in arr) {
arrMap.set(arr[k], k);
}
console.log(arrMap.get('489fa3682975da'));
console.log(arrMap.get('8653468te87a'));
console.log(arrMap.get('abcd123'));