1

I have below array of objects,

var parsedData =  [
        {
            "ID": "16",
            "DESCRIPTION": "SMAATO"
        },
        {
            "ID": "26",
            "DESCRIPTION": "BIDSWITCH"
        },
        {
            "ID": "1572",
            "DESCRIPTION": "BIDSWITCH"
        }
    ]

i have removed duplicate from this using below code,

var flags = [], l = parsedData.length, i;
            for( i=0; i<l; i++) {
                if( flags[parsedData[i].DESCRIPTION]){ 
                    if(attribute.toLowerCase() == "supply"){
                        console.log("coming!!"+parsedData[i].ID+"---"+parsedData[i].DESCRIPTION);                       
                    }
                    continue;                   
                }
                flags[parsedData[i].DESCRIPTION] = true;
                groups_array.push({     
                    id: parsedData[i].ID, 
                    text: parsedData[i].DESCRIPTION
                });                 
            }

but what i need to achive is, if id is differnt and description same means need to append id to first one and remove duplicate one like this,

[
    {
        "ID": "16",
        "DESCRIPTION": "SMAATO"
    },
    {
        "ID": "26,1572",
        "DESCRIPTION": "BIDSWITCH"
    }
]

How to get this one help me please...

1 Answer 1

1

Try:

if (flags[parsedData[i].DESCRIPTION]) {
    let id = groups_array.map(item => item.text).indexOf(parsedData[i].DESCRIPTION);
    groups_array[id].id = groups_array[id].id + "," + parsedData[i].ID
    continue;                   
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Its working.But the arrow function will support in all browsers?.In IE its not working.
If I'm not wrong, you can replace arrow function with: function(item){return item.text}. I've no idea If it'll work in all browsers, it's beyond my expertise, but googling 'arrow function browser support' should help. Perhaps, the details might be a topic for another, new SE question.

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.