0

Below is my JSON body.

{"key":"OTH","value":{"Name":"Other Countries"}}

I need to add this 'Other Countries' value as a last value in below array 'countryMap'

        if (state === "SUCCESS") {
            var result = response.getReturnValue();    
            var countryMap = [];
            for(var key in result){                    
                countryMap.push({key: key, value: result[key]});
            }                
            component.set("v.countryList", countryMap);
        }

Please help me out on this.

2
  • What is the issue you're facing? A quick lookup of documentation for array functions in Javascript suggests that push() is the function you're looking for.
    – Derek F
    Commented Dec 1, 2021 at 17:45
  • In array 'CountryMap', i'm getting below values in dropdown; Austria, Canada, France, Germany, Other Countries, United States Requirement : I have to add 'Other Countries' as the last value always. Expected Output: Austria, Canada, France, Germany, United States, Other Countries I've already tried everything but it didn;t work. Please help me on this. Commented Dec 1, 2021 at 17:49

1 Answer 1

0

You'll have to find and move the entry to the end yourself. Here's a simple example:

const countryMap = Object.entries(result) // converts Object to Array of [key, value] pairs
  .map(([key, value]) => ({ key, value })) // converts Array of [key, value] to Array of { key, value } objects
const other = countryMap.splice(countryMap.findIndex(value => value.key === 'OTH'), 1); // Find and remove OTH entry
countryMap.push(other)

There are plenty of other possible ways to write this as well, but it should be sufficient for your purposes.

Your JSON isn't very explanatory, so I hope I guessed correctly. If not, further changes may be needed to this code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .