1

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 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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'}

1 Comment

const array =[{ "2022-12-06": [{ "student": "10", "duration": "00:00:07", "intervals": "1" }] }] then I can use above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.