0

Given below is an object stored in variable nestedObject. Inside this object, you can see a property with keyname countries with 1 object value (switzerland). I want to create a function with 4 arguments: object,countryName, capital, population which adds a new key-value pair inside countries object and return the value of a newly inserted key. The key being countryName and value, an object with properties as shown in the example.

I want to Create a new function to add country with the name, capital, and population as parameters ---Then Call the function with given parameter values ---Return the result in the output variable

Given below is the format of the object which will be passed as the argument.

  speakers: [{ name: "Elie" }, { name: "Tim" }, { name: "Matt" }],
  data: {
    continents: {
      europe: {
        countries: {
          switzerland: {
            capital: "Bern",
            population: 38000000
          }
        }
      }
    },
    languages: {
      spanish: {
        hello: "Hola"
      },
      french: {
        hello: "Bonjour"
      }
    }
  }
}```
1

1 Answer 1

1

You can use Object.assign for this purpose (reference).

Your question can be done using assign as

var countryName={capital:capital,population:population};
const ans=Object.assign(object.data.continents.europe.countries,countryName);
Sign up to request clarification or add additional context in comments.

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.