0

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.

7
  • there is no duplicate object in your sample... could you be more explicit ? Commented May 20, 2019 at 0:30
  • Your example isn't valid JavaScript syntax. But in any case, if you know that the user has selected monday then all you have to do is access that property either with data.monday or data['monday'], or if you have a variable that contains the string you can use that -- data[yourVariable]. Commented May 20, 2019 at 0:32
  • Yes - but instead of using Arrays, use Sets. Commented May 20, 2019 at 0:32
  • @Herohtar that's my problem I don't know what the user select he has the choice to select any day of the week Commented May 20, 2019 at 0:33
  • 1
    So what you're actually asking is how to detect duplicates in that data structure and only add something if it is not already there. Commented May 20, 2019 at 0:49

2 Answers 2

1

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

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

1 Comment

yeah it works and thank you, I consider myself a newb in Es6 would you please tell me why you puted the day and the object in curly braces in the has object function parameters and thank you in advance
0

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

Comments

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.