简体   繁体   English

如何检查数组中 object 的键是否在 javascript 中具有特定值

[英]How to check if key of object in array has specific value in javascript

Is it possible to check if an object in an array has a specific Date value in the following data format?是否可以检查数组中的 object 是否具有以下数据格式的特定日期值?

I have an array like this for example例如,我有一个这样的数组

[
  {
    date: '2020-05-03',
    items: [{}...]
  },
  ...
]

-> In the above array, I want to check if any object in the array has '2020-05-03' as the date value.

And I want to find out if an object with Date is??我想知道一个带日期的 object 是否是?

I tried the following, but this only confirms that it has a 'date' as the key, and I couldn't compare values.我尝试了以下方法,但这只能确认它有一个“日期”作为键,我无法比较值。

const existsDate = _.find(prev, 'date');

I also want to push an item to the items of the object containing that date if a date already exists.如果日期已经存在,我还想将一个项目推送到包含该日期的 object 的项目中。

If not, i need to create a new date object.如果没有,我需要创建一个新日期 object。

You can make use of Array.prototype.find and do a string comparison您可以使用Array.prototype.find并进行字符串比较

var arr = [
  {
    date: '2020-05-03',
    items: [{}...]
  },
]

const obj = arr.find(item => item.data === '2020-05-03');

EDIT: Since you want to update the existing array, you would need to make use of slice with findIndex to update array编辑:由于要更新现有数组,因此需要使用带有 findIndex 的 slice 来更新数组

var arr = [
      {
        date: '2020-05-03',
        items: [{}...]
      },
    ]
    const newItem = {};
    const index= arr.findIndex(item => item.data === '2020-05-03');
    if(index > -1) {
       arr = [...arr.slice(0, index), {...arr[index], items: arr[index].items.concat(newItems), ...arr.slice(index + 1)}
    } else {
      arr.push({data: new Date(), items: [newItem]})
    }

You can use, Array find() , some() to get/check the required conditions, for example:您可以使用 Array find()some()来获取/检查所需的条件,例如:

 const arr = [ { date: '2020-05-03', items: [{}] }, { date: '2020-05-02', items: [{}] } ] function checkAndAdd(date) { const res = arr.find(ob => ob.date === date); // console.log(res); // based on the added comments. // if date is found, push something to the items, list: if (res) { res.items.push('Hello'); } else { arr.push({ date, items: [{}] }) } console.log('finalArray', arr); } checkAndAdd('2020-05-03'); checkAndAdd('2020-05-06');

you caan use filter or find function您可以使用过滤器或查找 function

let arr = [
    {data:'2020-05-23'},
    {data:'2020-06-23'},
    {data:'2020-07-23'}
]

let find = arr.find( (val) => val.data == '2020-06-23')

console.log(find)

I'm not sure what exactly you are looking to do but his code iterates through the array of objects and checks against the date variable.我不确定您到底要做什么,但他的代码遍历对象数组并检查日期变量。 It outputs the index (i).它输出索引 (i)。

let date = "2020-05-03"

const array = [

{   date: "2020-05-01"   },   
{   date: "2020-05-02"   },   
{   date: "2020-05-03",   }, 

]


for(let i = 0 ; i < array.length ; i ++) {   
    if(array[i].date === date) {
        console.log(i);   } 
    else {
        console.log("Date not in array");   
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM