Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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 - Fatal编程技术网

Javascript 从筛选数组中仅返回一个对象

Javascript 从筛选数组中仅返回一个对象,javascript,Javascript,我正在尝试创建一个注册表,用户应该在其中输入出生日期 但我有点困惑。为什么下面的代码返回12个对象的数组 let months=[ {月份:'一月',天数:31}, {月份:'二月',天数:28}, {月份:'三月',天数:31}, {月份:'四月',天数:30}, {月份:'五月',天数:31}, {月份:'六月',天数:30}, {月份:'七月',天数:31}, {月份:'八月',天数:31}, {月份:'九月',天数:30}, {月份:'十月',天数:31}, {月份:'11月',天数:

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

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

let months=[
{月份:'一月',天数:31},
{月份:'二月',天数:28},
{月份:'三月',天数:31},
{月份:'四月',天数:30},
{月份:'五月',天数:31},
{月份:'六月',天数:30},
{月份:'七月',天数:31},
{月份:'八月',天数:31},
{月份:'九月',天数:30},
{月份:'十月',天数:31},
{月份:'11月',天数:30},
{月份:12月,天数:31}
];
月份='四月';
设a=Array.from(月,(值,索引)=>value);
设b=a.filter(值=>{
返回值.month=month;
});
控制台日志(b)更改为

let b = a.filter(value => {
  return value.month == month;
});
在你分配任务之前。以上==比较。

更改为

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

在你分配任务之前。上面的==比较。

您正在进行赋值,而不是比较。 应该是

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

你是在做作业,而不是比较。 应该是

let b = a.filter(value => {
 return value.month == month;
});
您使用了
=
而不是
=

您使用了
=
而不是
=

在数组中使用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;

在数组中使用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;


试试下面的代码snippat

let months=[
{月份:'一月',天数:31},
{月份:'二月',天数:28},
{月份:'三月',天数:31},
{月份:'四月',天数:30},
{月份:'五月',天数:31},
{月份:'六月',天数:30},
{月份:'七月',天数:31},
{月份:'八月',天数:31},
{月份:'九月',天数:30},
{月份:'十月',天数:31},
{月份:'11月',天数:30},
{月份:12月,天数:31}
];
月份='四月';
设a=Array.from(月,(值,索引)=>value);
设b=a.filter(值=>{
如果(value.month==月){
返回值.month;
}
});

控制台日志(b)尝试下面的代码snippat

let months=[
{月份:'一月',天数:31},
{月份:'二月',天数:28},
{月份:'三月',天数:31},
{月份:'四月',天数:30},
{月份:'五月',天数:31},
{月份:'六月',天数:30},
{月份:'七月',天数:31},
{月份:'八月',天数:31},
{月份:'九月',天数:30},
{月份:'十月',天数:31},
{月份:'11月',天数:30},
{月份:12月,天数:31}
];
月份='四月';
设a=Array.from(月,(值,索引)=>value);
设b=a.filter(值=>{
如果(value.month==月){
返回值.month;
}
});
控制台日志(b)试试这个

 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);
试试这个

 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);

=
用于赋值,
=
==
用于比较。条件应为
value.month===month而不是
value.month=month
=
是assignment.LOL@非常感谢。
=
用于赋值,
=
==
用于比较。条件应为
value.month===month而不是
value.month=month
=
是assignment.LOL@哈桑:非常感谢。太棒了:)Thanks@Sanjay请向上投票,并在7分钟内给绿色支票,如果这是你想要的答案。真棒:)Thanks@Sanjay请在7分钟内投票并给出绿色支票,如果这是你想要的答案。