简体   繁体   中英

Is there a Javascript function to take an object as argument and return an array of strings

I desire to have a javascript function that can take an object with properties as an argument, process it and return the object's properties as an array of strings... The code am trying so far is here but its printing out the entry name and the property name

let Southampton={
    name:"Southampton",
    founded:1900,
    stadium:"St. Mary's Stadium",
    points:36
}
function printObj(arg) {
  for (let [key, value] of Object.entries(arg)) {
    console.log(`${key}: ${value}`);
  }
}
function printObj(Southampton)

The code above is outputting

name: Southampton
founded: 1900
stadium: St. Mary's Stadium
points: 36

Can the function be extra modified to make it return this

Southampton was founded in 1900

Thank You for your assistance

Use arg.map() to return an array from calling a function on each entry.

function obj_to_array(arg) {
    return Object.entries(arg).map(([key, value]) => `${key}: ${value}`);
}

To get the second message, you can't use a general purpose function like that, since it needs to format the string specifically for the founded property.

function when_founded(arg) {
    return `${arg.name} was founded in ${arg.founded}`;
}

If you really want them both in a single function, you can print the message before returning the array.

 let Southampton = { name: "Southampton", founded: 1900, stadium: "St. Mary's Stadium", points: 36 }; let Burnley = { name: "Burnley", founded: 1850, stadium: "Turf Moor", points: 33 }; let cities = [Southampton, Burnley]; function when_founded(arg) { return `${arg.name} was founded in ${arg.founded}`; } function obj_to_array(arg) { console.log(when_founded(arg)) return Object.entries(arg).map(([key, value]) => `${key}: ${value}`); } console.log(obj_to_array(Southampton)); console.log(cities.map(when_founded))

There's no build-in function for this. If i understand it correctly, to achieve your goal you just need to use string interpolation to display parameters, like:

function printObj(obj) {
 return `${obj.name} is founded in ${obj.founded}.`;
}

As long as you know the index's of each word you could do something like this:

 let Southampton = { name: "Southampton", founded: 1900, stadium: "St. Mary's Stadium", points: 36 } function printObj(arg) { let values = []; for (let [key, value] of Object.entries(arg)) { values.push(key); values.push(value); } console.log(`${values[1]} was founded in ${values[3]}`); } printObj(Southampton); // Southampton was founded in 1900

With Object.values() Method you could do the basic work as an oneliner. ( NO IE Support)

const valArray = Object.values(Southampton);

console.log(valArray);

console.log(`${valArray[0]} is founded in ${valArray[1]}`);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM