skip to Main Content

I have array=

[{

        "2022-12-06": [{
            "student": "10",
            "duration": "00:00:07",
            "intervals": "1"
        }]
    },
    {

        "2022-09-08": [{
                "student": "20",
                "duration": "00:00:07",
                "intervals": "1"
            },
            {
                "student": "300",
                "duration": "00:00:07",
                "intervals": "1"
            },
            {
                "student": "10",
                "duration": "00:00:07",
                "intervals": "1"
            }
        ]
    },
    {

        "date20221208": [{
                "student": "10",
                "duration": "00:00:07",
                "intervals": "1"
            },
            {
                "student": "10",
                "duration": "00:00:07",
                "intervals": "1"
            },
            {
                "student": "10",
                "duration": "00:00:07",
                "intervals": "1"
            },
            {
                "student": "10",
                "duration": "00:00:07",
                "intervals": "1"
            },
            {
                "student": "10",
                "duration": "00:00:07",
                "intervals": "1"
            }

        ]
    }
]

I want find a date and return value date is exits or not
for eg.
i find 2022-12-06 if array have this value then true if not then false

I want find a date and return value date is exits or not
for eg.
i find 2022-12-06 if array have this value then true if not then false

2

Answers


  1. you can use the Array.prototype.find() method to search for an object in an array. This method returns the first element in the array that satisfies the provided testing function.

    For example, if you have an array of objects and you want to find an object with a specific date property, you could use the find() method like this:

    const array = [{date: '2022-12-06', name: 'John'}, {date: '2020-02-05', name: 'Jane'}, {date: '1990-04-07', name: 'Bob'}];
    
    const object = array.find(obj => obj.date === '2022-12-06');
    
    console.log(object); // {date: '2022-12-06', name: 'John'}
    
    Login or Signup to reply.
  2. First of all this question is not about react-native. This is about basic javascript.

    You can use Array.Prototype.find() method.

    const arr = [{
    
            "2022-12-06": [{
                "student": "10",
                "duration": "00:00:07",
                "intervals": "1"
            }]
        },
        {
    
            "2022-09-08": [{
                    "student": "20",
                    "duration": "00:00:07",
                    "intervals": "1"
                },
                {
                    "student": "300",
                    "duration": "00:00:07",
                    "intervals": "1"
                },
                {
                    "student": "10",
                    "duration": "00:00:07",
                    "intervals": "1"
                }
            ]
        },
        {
    
            "date20221208": [{
                    "student": "10",
                    "duration": "00:00:07",
                    "intervals": "1"
                },
                {
                    "student": "10",
                    "duration": "00:00:07",
                    "intervals": "1"
                },
                {
                    "student": "10",
                    "duration": "00:00:07",
                    "intervals": "1"
                },
                {
                    "student": "10",
                    "duration": "00:00:07",
                    "intervals": "1"
                },
                {
                    "student": "10",
                    "duration": "00:00:07",
                    "intervals": "1"
                }
    
            ]
        }
    ]
    
    const checkArray = (str) => {
      const x = arr.find(item => item.hasOwnProperty(str))
      return x ? true:false
      // or return object.If has not return undefined
      // return x
    }
    
    console.log(checkArray("2022-12-06"))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search