简体   繁体   English

javascript 将变量分配给从 object 中提取的键值

[英]javascript assign variable to key-values extracted from object

how can i do something like the following我怎么能做类似以下的事情

const BigObject = {
"id":"value",
"img":"value",
"name":"value",
"otherData":"value",
"otherData":"value",
"otherData":"value",
"otherData":"value",

}
var User = {id,img,name} = BigObject

where User will be an object like其中用户将是 object 之类的

{
    "id":"value",
    "img":"value",
    "name":"value",
}

From my above comment...从我上面的评论...

"An approach like... const user = (({ id, img, name }) => ({ id, img, name }))(BigObject); ... which is based on an immediately invoked arrow function prevents additional local references which are not needed anymore after the creation of user ." “类似... const user = (({ id, img, name }) => ({ id, img, name }))(BigObject); ... 的方法基于立即调用的箭头 function创建user后不再需要的本地引用。”

Implementing the solution with an arrow function might also come closest to the OP'S original intention...用箭头 function 实现解决方案也可能最接近 OP 的初衷......

 const bigObject = { id: 'value', img: 'value', name: 'value', otherData: 'value', }; // OP...how can i do something like the following?.. // // const user = { id, img, name } = bigObject // prevent additional module or global // scope of eg `id`, `img`, `name`. const user = (({ id, img, name }) => ({ id, img, name }))(bigObject); console.log({ user });

You can try it:你可以试试:

const {id, img, name} = BigObject;
const User = {id, img, name};

You could do it like below:你可以这样做:

 const BigObject = { "id":"value", "img":"value", "name":"value", "otherData":"value", "otherData":"value", "otherData":"value", "otherData":"value" } let {id, img, name, ...rest} = BigObject; let User = {id,img,name}; console.log(User)

you can do something like this你可以做这样的事情

const BigObject = {
"id":"value",
"img":"value",
"name":"value",
"otherData":"value",
"otherData":"value",
"otherData":"value",
"otherData":"value",

}
let {id,img,name} = BigObject
const User = {id,img,name}
console.log(User)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中生成键值反转对象? - How to generate an key-values inversed object in JavaScript? 从 JSON 响应创建 object 删除不需要的键值 - Create object from JSON response remove unwanted key-values 在javascript中复制部分json键值 - Copy part of json key-values in javascript 将动态键值添加到JSON对象 - Add dynamic key-values to JSON object 从一个 Array 对象中提取键值,并使用带下划线 javascript 的提取值从其他对象中过滤 - Extract key values from one Array object and filter from other objects with the extracted values with underscore javascript 从JSON提取键值数组 - Extract array of key-values from JSON 从对象中提取所需的键和值并将其分配给 Javascript 中的新对象 - Extract Required Key and Values from the Object and assign it to New Object in Javascript JavaScript JSON object 中的递归访问并重命名孩子的一些键值 - JavaScript recursive access in JSON object and rename some key-values on children JS 映射 object 选择嵌套数组键值以使它们成为更高级别的键值 - JS Mapping object picking nested array key-values to make them higher level key-values 从对象数组获取键值数组,而又不知道对象数组的格式(Javascript)? - Get array of key-values from array of objects without knowing format of array of objects (Javascript)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM