简体   繁体   中英

Loop through an object of arrays that contains objects

Is there a way that lets me loop through an object of arrays that contains objects?

I think it looks weird. Let me give you an example:

data {
  monday = [
    {from:"55:00", to:"12:00", txt: "hello"},
    {from:"55:00", to:"12:00", txt: "study"},
    {from:"55:00", to:"12:00", txt: "play"}
  ],
  tuesday = [
    {from:"7:00", to:"11:00", txt: "watch"},
    {from:"09:00", to:"13:00", txt: "swim"},
  ]
}

Let's suppose that the user picks a day from a select option and that day will decide in which array of the data object the data that the user will input will be saved.

Is there a way that lets me prevent duplicated objects from being saved in the data object arrays?

I don't know why I feel it's not clear yet but here is another example: If the user picks the monday day, the inputs that he is going to input will be saved in the monday array as parameters of an object. I want to know if there is a way that lets me prevent a duplicated object in that array.

That's why I want to loop through them. I googled that issue and I found some solutions like the for..in but I don't think they fit my question. Thank you in advance.

The object is invalid, assuming that the object is the following:

const data = {
    monday: [
        { from: '55:00', to: '12:00', txt: 'hello' },
        { from: '09:00', to: '13:00', txt: 'study' },
        { from: '55:00', to: '12:00', txt: 'play' }
    ],
    tuesday: [
        { from: '7:00', to: '11:00', txt: 'watch' },
        { from: '09:00', to: '13:00', txt: 'swim' }
    ]
};

You can try this validation function

function hasObject({ day, object }) {
    const dataset = data[day];

    return dataset.some(data => {
        return (
            data.from === object.from &&
            data.to === object.to &&
            data.txt === object.txt
        );
    });
}

Using Array​.prototype​.some()

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

Please try this example

 const data = { monday: [ { from: '55:00', to: '12:00', txt: 'hello' }, { from: '09:00', to: '13:00', txt: 'study' }, { from: '55:00', to: '12:00', txt: 'play' } ], tuesday: [ { from: '7:00', to: '11:00', txt: 'watch' }, { from: '09:00', to: '13:00', txt: 'swim' } ] }; function hasObject({ day, object }) { const dataset = data[day]; return dataset.some(entry => { return ( entry.from === object.from && entry.to === object.to && entry.txt === object.txt ); }); } const result = hasObject({ day: 'monday', object: { from: '55:00', to: '12:00', txt: 'hello' } }); console.log(result); 

The result is true, because the object { from: '55:00', to: '12:00', txt: 'hello' } is in the list of objects of the day.

I hope I have interpreted your case properly

You can do something like this:

 let data = { monday: [{ from: "55:00", to: "12:00", txt: "hello" }, { from: "09:00", to: "13:00", txt: "study" }, { from: "55:00", to: "12:00", txt: "play" } ], tuesday: [{ from: "7:00", to: "11:00", txt: "watch" }, { from: "09:00", to: "13:00", txt: "swim" }, ] } let selectedDay = 'monday' let dubData = { from: "09:00", to: "13:00", txt: "study"} let cleanData = { from: "19:00", to: "21:00", txt: "FOO"} let areSame = (o1,o2) => Object.entries(o1).every(([k,v]) => o2[k] === v) let hasEntry = (arr, obj) => arr.some(x => areSame(obj, x)) console.log(hasEntry(data[selectedDay], dubData)) console.log(hasEntry(data[selectedDay], cleanData)) 

Where you would create a function to compare objects if they are the same ( areSame function - note this is a VERY simplistic comparison based on your use case. This would compare only primitive values etc). Another function hasEntry would check if there is a duplicate object in an array.

So you would just get the needed data set slice based on the selected day (in this case monday - like data[selectedDay] ) and pass with the new object to the hasEntry function for validation.

This utilizes Array.some , Array.every and Object.entries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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