简体   繁体   中英

Object destructing of many elements

I want to use object destructuring of an object with many keys with almost the same name. Is there a way to dynamically extract them if I know that there are a finite number of keys?

const {status, name1, name2, name50 } = req.body;
console.log(name12) // > john

I hope I´m explaining myself correctly. thank you.

Simply using the rest operator on the remaining props after you pluck out the status may be what you're going for. In the following snippet, we destructure out the status and put the rest of the props in a names object.

 const body = { status: 200, name1: "John", name2: "Joe", name3: "Bob" } const { status, ...names } = body; console.log(names);

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