我想找到包含多个对象的数组中的对象的索引。我一直在努力争取了一段时间,并将它简化为你所看到的并且仍然无法获得的东西。请让我知道什么工作,这样我可以从matchBetween("string1");
findIndex javascript问题
matchBetween("string1");
matchBetween = function (result) {
let params = [
{ param: "string1", input: "inputstring1"},
{ param: "string2", input: "inputstring2"}
];
console.log(result, params, params.param); //Output: "string1", (2)[{...}, {...
let location = params.findIndex (x => Object.is(result, x));
console.log(location); //outputs -1
return params[location].input // 'Cannot read property 'input' of undefined'
};
我已经尝试多种事态“输入”对象的字符串,但我觉得这是一个简单的办法,我只是缺少它。先谢谢你!
你是Object.is将每个{param,input}对象与一个字符串进行比较...因此,显然没有匹配...'x => x.param == result'而不是 –
note:'params .param'将始终未定义,因为这不是对象数组的作用方式 –