Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用数组从对象数组中提取值_Javascript_Arrays_Object_Ecmascript 6 - Fatal编程技术网

Javascript 如何使用数组从对象数组中提取值

Javascript 如何使用数组从对象数组中提取值,javascript,arrays,object,ecmascript-6,Javascript,Arrays,Object,Ecmascript 6,我有一系列密码验证失败的原因: const failures = schema.validate(password.value, { list: true }); console.log(failues); // => [ 'min', 'uppercase', 'digits' ] 我还有一个对象数组。键始终是故障数组的一个潜在值: 我想映射failures数组并从msg返回相关消息,以显示更连贯的错误消息。对于failures中的每个值,从msg中提取相应的项。 但是,我可以建议将m

我有一系列密码验证失败的原因:

const failures = schema.validate(password.value, { list: true });
console.log(failues);
// => [ 'min', 'uppercase', 'digits' ]
我还有一个对象数组。键始终是故障数组的一个潜在值:

我想映射failures数组并从msg返回相关消息,以显示更连贯的错误消息。

对于failures中的每个值,从msg中提取相应的项。 但是,我可以建议将msg创建为dict吗

常数msg={ “最小值”:“最小值”, “最大值”:“最大值”, ... }; 这样做会更容易

failures.forEachfailure=>{ 如果msg中出现故障{ console.logmsg[failure]; } }; 对于failures中的每个值,从msg中提取相应的项。 但是,我可以建议将msg创建为dict吗

常数msg={ “最小值”:“最小值”, “最大值”:“最大值”, ... }; 这样做会更容易

failures.forEachfailure=>{ 如果msg中出现故障{ console.logmsg[failure]; } }; 您可以使用array.filter和array.map实现所需的功能:

const messages = msg.filter(r=>failures.indexOf(r.key)!==-1).map(r=>r.message)// ["minimum", "need a uppercase", "must have digits"]
从这里开始如何处理它取决于您,因为即使您加入消息数组,它也不会形成一个连贯的句子。我也同意上面的建议,即切换到类似Dict的对象,而不是现在使用的对象数组

您可以使用array.filter和array.map实现您想要的:

const messages = msg.filter(r=>failures.indexOf(r.key)!==-1).map(r=>r.message)// ["minimum", "need a uppercase", "must have digits"]
const failures = schema.validate(password.value, { list: true });
console.log(failures);
// => [ 'min', 'uppercase', 'digits' ]

const msg = [
  { key: 'min', message: 'minimum' },
  { key: 'max', message: 'maximum' },
  { key: 'uppercase', message: 'need a uppercase' },
  { key: 'lowercase', message: 'need a lowercase' },
  { key: 'digits', message: 'must have digits' },
  { key: 'spaces', message: 'no spaces' },
  { key: 'oneOf', message: 'is not one of' },
];

const listOfMessages = msgs
  .filter(msg => failures.includes(msg.key)) // Get the appropriate object matching the error
  .map(msg => msg.message) // Only get the message

console.log(listOfMessages) // ['minimum', 'need a uppercase', 'must have digits']
从这里开始如何处理它取决于您,因为即使您加入消息数组,它也不会形成一个连贯的句子。我也同意上面的建议,即切换到类似Dict的对象,而不是现在使用的对象数组

const failures = schema.validate(password.value, { list: true });
console.log(failures);
// => [ 'min', 'uppercase', 'digits' ]

const msg = [
  { key: 'min', message: 'minimum' },
  { key: 'max', message: 'maximum' },
  { key: 'uppercase', message: 'need a uppercase' },
  { key: 'lowercase', message: 'need a lowercase' },
  { key: 'digits', message: 'must have digits' },
  { key: 'spaces', message: 'no spaces' },
  { key: 'oneOf', message: 'is not one of' },
];

const listOfMessages = msgs
  .filter(msg => failures.includes(msg.key)) // Get the appropriate object matching the error
  .map(msg => msg.message) // Only get the message

console.log(listOfMessages) // ['minimum', 'need a uppercase', 'must have digits']
//这是一种非常冗长的信息存储方式,也很难通过键获取 常数msg=[ {键:'min',消息:'minimum'}, {键:'max',消息:'max'}, {键:'大写',消息:'需要大写'}, {键:'小写',消息:'需要小写'}, {键:'数字',消息:'必须有数字'}, {键:“空格”,消息:“无空格”}, {key:'oneOf',message:'不是'}之一, ]; 常量失败=[ "敏",, “大写”, “数字”, “未映射的对象” ]; //最好用地图 const msg2=msg.reduceac,item=>{ acc[item.key]=item.message; 返回acc; }, {}; console.logmsg2:,msg2; //现在您可以执行以下操作: console.logmapped failures:,failures.mapkey=>msg2[key]| | key; .作为控制台包装{top:0;最大高度:100%!important} //这是一种非常冗长的信息存储方式,也很难通过键获取 常数msg=[ {键:'min',消息:'minimum'}, {键:'max',消息:'max'}, {键:'大写',消息:'需要大写'}, {键:'小写',消息:'需要小写'}, {键:'数字',消息:'必须有数字'}, {键:“空格”,消息:“无空格”}, {key:'oneOf',message:'不是'}之一, ]; 常量失败=[ "敏",, “大写”, “数字”, “未映射的对象” ]; //最好用地图 const msg2=msg.reduceac,item=>{ acc[item.key]=item.message; 返回acc; }, {}; console.logmsg2:,msg2; //现在您可以执行以下操作: console.logmapped failures:,failures.mapkey=>msg2[key]| | key;
.as控制台包装器{top:0;max height:100%!important}非常有用。谢谢你:这是一种享受。谢谢您: