skip to Main Content

I have an object with section/fields as below:

{
    "section-1": {
        "fields": [
            {"id": "field1", value: "Field1"},
            {"id": "field2", value: "Field2"}
        ]
    }, 
    "section-2": {
        "fields": [
            {"id": "field3", value: "Field3"},
            {"id": "field4", value: "Field4"}
        ]
    }, 
    "section-3": {
        "fields": [
            {"id": "field5", value: "Field5"},
            {"id": "field6", value: "Field6"}
        ]
    }
}

Now there is a dynamic array which I get as below:

['field1', 'field5']

What I want to is iterate over the above array items (i.e. 'field1' and 'field5') and find the corresponding section inside which the field id is present.

So for the above example, I want the output as below:

['section-1', 'section-3']

Is there any ES6 way to do the same?

3

Answers


  1. Edit: This kind of questions are best answered by generative AI.
    Here is the answer by Codeium

    The one provided by SO gave: "
    Unfortunately, we aren’t able to locate any information about this topic on Stack Overflow"

    Yes, you can achieve this using ES6 methods like Object.entries(),
    Array.find(), and Array.map(). Here’s an example of how you can do it:

    In the above code, we use Object.entries(data) to convert the data
    object into an array of key-value pairs. Then, we use Array.find() to
    find the first entry where the fields array contains a field with the
    given fieldId. Finally, we use Array.map() to extract the section key
    from each found entry and store it in the sections array.

    const data = {
        "section-1": {
            "fields": [
                {"id": "field1", value: "Field1"},
                {"id": "field2", value: "Field2"}
            ]
        }, 
        "section-2": {
            "fields": [
                {"id": "field3", value: "Field3"},
                {"id": "field4", value: "Field4"}
            ]
        }, 
        "section-3": {
            "fields": [
                {"id": "field5", value: "Field5"},
                {"id": "field6", value: "Field6"}
            ]
        }
    };
    
    const dynamicArray = ['field1', 'field5'];
    
    const sections = dynamicArray.map(fieldId => {
        const [section] = Object.entries(data).find(([section, { fields }]) =>
            fields.some(field => field.id === fieldId)
        );
        return section;
    });
    
    console.log(sections); // Output: ['section-1', 'section-3']
    Login or Signup to reply.
  2. filter() the Object.keys() where you check if some() of the field.id includes() in your to-find-array.

    const data = {
        "section-1": {
            "fields": [
                {"id": "field1", value: "Field1"},
                {"id": "field2", value: "Field2"}
            ]
        }, 
        "section-2": {
            "fields": [
                {"id": "field3", value: "Field3"},
                {"id": "field4", value: "Field4"}
            ]
        }, 
        "section-3": {
            "fields": [
                {"id": "field5", value: "Field5"},
                {"id": "field6", value: "Field6"}
            ]
        }
    }
    
    const ids = ['field1', 'field5'];
    
    const res = Object.keys(data).filter(k => data[k].fields.some(f => ids.includes(f.id)));
    console.log(res);
    Login or Signup to reply.
  3. You can do this by using Object.keys and find methods in combination with some array iteration

    const data = {
        "section-1": {
            "fields": [
                {"id": "field1", value: "Field1"},
                {"id": "field2", value: "Field2"}
            ]
        },
        "section-2": {
            "fields": [
                {"id": "field3", value: "Field3"},
                {"id": "field4", value: "Field4"}
            ]
        },
        "section-3": {
            "fields": [
                {"id": "field5", value: "Field5"},
                {"id": "field6", value: "Field6"}
            ]
        }
    };
    
    const dynamicArray = ['field1', 'field5'];
    
    const result = dynamicArray.map(fieldId =>
        Object.keys(data).find(section =>
            data[section].fields.some(field => field.id === fieldId)
        )
    );
    
    console.log(result);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search