0

Is it possible to check if an object in an array has a specific Date value in the following data format?

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 ??

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.

If not, i need to create a new date object.

COLEAN
  • 665
  • 2
  • 9
  • 24
  • 2
    What have you already tried? It's a good idea to include what you've already tried doing to overcome this, and what problems you're facing. This will make your question more focused and easier to answer concisely. Please take the [Tour](//stackoverflow.com/tour) and review [How do I ask a good question?](//stackoverflow.com/help/how-to-ask). – Tad Wohlrapp May 03 '20 at 15:47
  • use `filter` or `find` – brk May 03 '20 at 15:47
  • 1
    Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – alexortizl May 03 '20 at 15:51

4 Answers4

0

You can make use of Array.prototype.find and do a string comparison

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

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]})
    }
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
0

You can use, Array find(), some() to get/check the required conditions, for example:

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');
Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51
0

you caan use filter or find 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)
Khairu Aqsara
  • 1,321
  • 3
  • 14
  • 27
0

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

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");   
    } 
}
Mike B.
  • 123
  • 8