简体   繁体   English

通过名称将属性从对象中拉到另一个对象中

[英]Pull properties from an object by their name into a different object

i'm doing a project in VueJS and i have an array of data, which consists of a number of objects. 我正在VueJS中做一个项目,我有一个数据数组,其中包含许多对象。

These objects are pulled from a PHP Backend and consist of values like 这些对象是从PHP后端提取的,包含如下值

id: 2123 name: "Name Value" status: "active" account_id: "2KGGALS2353255" id:2123名称:“名称值”状态:“活动” account_id:“ 2KGGALS2353255”

Imagine i want to split these by the keys names into a similar array but i want to have a parent object that consists of two child objects 想象一下,我想通过键名将它们拆分成类似的数组,但是我想要一个由两个子对象组成的父对象

[
    0: {
     core: {
       id: 2123
       name: "Name Value"
     },
     extra: {
       status: "active",
       account_id: "2KGGALS2353255" 
     }
]

The question is how can i achieve this with Javascript? 问题是如何使用Javascript实现此目的? I don't really want to modify the data in PHP beforehand unless this is something very unadvised to do in Javascript. 我真的不想事先在PHP中修改数据,除非这是极不建议在Javascript中进行的操作。 I can use VueJS and Lodash. 我可以使用VueJS和Lodash。

I was looking for lodash's pick() method. 我在寻找lodash的pick()方法。

https://lodash.com/docs/4.17.4#pick https://lodash.com/docs/4.17.4#pick

This should work for your purpose 这应该适合您的目的

 function separate(obj, keys) { let target = {}, rest = {}; Object.keys(obj).forEach(function(key) { if (keys.includes(key)) { target[key] = obj[key]; } else { rest[key] = obj[key]; } }); return { target: target, rest: rest }; } let stuff = { id: 2123, name: "Name Value", status: "active", account_id: "2KGGALS2353255" }; let separated = separate(stuff, ['id', 'name']); console.log({ core: separated.target, extra: separated.rest }); 

Using ES6's object destructuring , and the object rest spread proposal, which requires a babel transform , you can Array#map the array into a new array of objects in the required format: 使用ES6的对象解构和需要babel变换对象剩余散布建议,您可以将Array#map为所需格式的新对象数组:

 const arr = [{"id":1,"name":"Name1","status":"active","account_id":"2KGGALS2353255"},{"id":2,"name":"Name2","status":"active","account_id":"4ABCLS2353255"},{"id":3,"name":"Name3","status":"active","account_id":"6LMNALS2353255"}]; const result = arr.map(({ id, name, ...extra }) => ({ core: { id, name }, extra })); console.log(result); 

You can do the same thing using lodash's _.pick() to the get the core , and _.omit() to get the extra : 您可以使用lodash的_.pick()来获取内核 ,并使用_.omit()来获取额外的内容

 var arr = [{"id":1,"name":"Name1","status":"active","account_id":"2KGGALS2353255"},{"id":2,"name":"Name2","status":"active","account_id":"4ABCLS2353255"},{"id":3,"name":"Name3","status":"active","account_id":"6LMNALS2353255"}]; var result = arr.map(function(obj) { return { core: _.pick(obj, ['id', 'name']), extra: _.omit(obj, ['id', 'name']) }; }); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> 

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

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