仅从过滤的数组中返回一个对象

时间:2018-04-07 14:06:53

标签: javascript

我正在尝试创建一个注册表单,用户应该在其中输入出生日期。

但我有点困惑。为什么下面的代码返回12个对象的数组?



let months= [
      {month: 'January', days: 31},
      {month: 'February', days: 28},
      {month: 'March', days: 31},
      {month: 'April', days: 30},
      {month: 'May', days: 31},
      {month: 'June', days: 30},
      {month: 'July', days: 31},
      {month: 'August', days: 31},
      {month: 'September', days: 30},
      {month: 'October', days: 31},
      {month: 'November', days: 30},
      {month: 'December', days: 31}
];

month = 'April';

let a = Array.from(months, (value, index) =>value);

let b = a.filter(value => {
  return value.month = month;
});
console.log(b);




我只是想要{month: 'April', days: 30},但程序会返回12个不同日期的对象。请帮忙。

6 个答案:

答案 0 :(得分:2)

在数组中使用find

let months= [
      {month: 'January', days: 31},
      {month: 'February', days: 28},
      {month: 'March', days: 31},
      {month: 'April', days: 30},
      {month: 'May', days: 31},
      {month: 'June', days: 30},
      {month: 'July', days: 31},
      {month: 'August', days: 31},
      {month: 'September', days: 30},
      {month: 'October', days: 31},
      {month: 'November', days: 30},
      {month: 'December', days: 31}
];

monthSearched = 'April';

let a = Array.from(months, (value, index) =>value);

let b = a.find(value => {
  return value.month = monthSearched;
});
console.log(b);

或只是改变你的

return value.month = monthSearched;

return value.month === monthSearched;

答案 1 :(得分:1)

更改为

let b = a.filter(value => {
  return value.month == month;
});

在分配之前。以上==比较。

答案 2 :(得分:0)

您正在进行作业而不是比较。 它应该是

let b = a.filter(value => {
 return value.month == month;
});

答案 3 :(得分:0)

let months= [
      {month: 'January', days: 31},
      {month: 'February', days: 28},
      {month: 'March', days: 31},
      {month: 'April', days: 30},
      {month: 'May', days: 31},
      {month: 'June', days: 30},
      {month: 'July', days: 31},
      {month: 'August', days: 31},
      {month: 'September', days: 30},
      {month: 'October', days: 31},
      {month: 'November', days: 30},
      {month: 'December', days: 31}
];

month = 'April';

let a = Array.from(months, (value, index) =>value);

let b = a.filter(value => {
  return value.month == month;
});
console.log(b);

您使用=代替==

答案 4 :(得分:0)

尝试下面的代码snippat,

let months= [
      {month: 'January', days: 31},
      {month: 'February', days: 28},
      {month: 'March', days: 31},
      {month: 'April', days: 30},
      {month: 'May', days: 31},
      {month: 'June', days: 30},
      {month: 'July', days: 31},
      {month: 'August', days: 31},
      {month: 'September', days: 30},
      {month: 'October', days: 31},
      {month: 'November', days: 30},
      {month: 'December', days: 31}
];

month = 'April';

let a = Array.from(months, (value, index) =>value);

let b = a.filter(value => {
  if(value.month == month){
  return value.month;
  }
});
console.log(b);

答案 5 :(得分:0)

试试这个

 let months= [
  {month: 'January', days: 31},
  {month: 'February', days: 28},
  {month: 'March', days: 31},
  {month: 'April', days: 30},
  {month: 'May', days: 31},
  {month: 'June', days: 30},
  {month: 'July', days: 31},
  {month: 'August', days: 31},
  {month: 'September', days: 30},
  {month: 'October', days: 31},
  {month: 'November', days: 30},
      {month: 'December', days: 31}
  ];

month = 'April';

let a = Array.from(months, (value, index) =>value);

let b = a.filter(value => {
  return value.month == month; // use == instead of = filter callback should return a boolean 
});
console.log(b);