2017-08-12 82 views
0

我有一个对象,我试图映射到一个反应组件(使用lodash)。我回来从我的API(火力)的对象的当前形状看起来像这样...用map重构一个对象for each

// ex. 1 
{ 
    "-Kdkahgiencls0dnh": { 
    "name": "a name", 
    "desc": "a description", 
    "other": "some other guff" 
    }, 
    "-Ksdfadfvcls0dsnh": { 
    "name": "another name", 
    "desc": "another description", 
    "other": "some more" 
    }, 
    "-kgoandiencls0dnh": { 
    "name": "I am a name", 
    "desc": "I am a description", 
    "other": "I am some other guff" 
    } 
} 

...但是,我失去了主键,当我通过_.map()

运行我“M试图做的是让我的物体的形状:

// ex. 2 
[ 
    { 
    "id": "-Kdkahgiencls0dnh", 
    "name": "a name", 
    "desc": "a description", 
    "other": "some other guff" 
    }, 
    {... the next object ...}, 
    {... etc ...} 
] 

我在做什么,现在是让我的数据在componentWillMount生命周期的方法,像这样:

componentWillMount() { 
    firebaseRef.on('value', snap => { 
    let data = snap.val() // the whole original object (see ex. 1) 
    let tempArray = [] // an array to store my newly formatted objects 
    _.forEach(data, (item, key) => { 
     // Here's where i'm not really sure what to do. 
     // I want to use Object.assign to set a new key:value 
     // That adds "id": "-theobjectsmainkey" to a new object 
     // then push to my tempArray and finally setState with the 
     // correctly formatted array of objects. 
    }) 
    }) 
} 

想法?思考?谢谢。

回答

2

您可以使用Object.entries().map()和对象传播

const data = { 
 
    "-Kdkahgiencls0dnh": { 
 
    "name": "a name", 
 
    "desc": "a description", 
 
    "other": "some other guff" 
 
    }, 
 
    "-Ksdfadfvcls0dsnh": { 
 
    "name": "another name", 
 
    "desc": "another description", 
 
    "other": "some more" 
 
    }, 
 
    "-kgoandiencls0dnh": { 
 
    "name": "I am a name", 
 
    "desc": "I am a description", 
 
    "other": "I am some other guff" 
 
    } 
 
} 
 

 
let res = Object.entries(data).map(([id, prop]) => ({id, ...prop})); 
 

 
console.log(res);

+0

YAS !!!这正是我所追求的,我希望自己有一个更好的把握这在做什么... – archae0pteryx

+0

@ archae0pteryx请参阅[Object.entri es()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries),[解构赋值](https://developer.mozilla.org/en -US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment),[Spread语法](https://developer.mozilla。org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator), – guest271314

+0

这非常有帮助。谢谢! – archae0pteryx

1
componentWillMount() { 
    firebaseRef.on('value', snap => { 
    let data = snap.val() // the whole original object (see ex. 1) 
    let tempArray = Object.keys(data).map((item, key) => { 
     return { 
      "id": item, 
      "name": data[item].name // etc, a structure what you want 
      ... 
     }; 
    }) 
    }) 
} 
+0

第一个例子,给出:'''[ { “desc” 的: “首先描述”, “字幕”: “第一子标题”, “标题”: “首先,项目名称” } , { “desc”:“这里不需要描述。没什么可看“ ‘副标题:众神的字幕‘ ‘标题’’’:‘另一个项目’ }, { ‘说明’:‘如果我有一个递减它会去这里’, ”副标题“:‘我是字幕听我怒吼’, ‘称号’:‘最后的工程实例’ } ]''' – archae0pteryx

+0

这不添加id属性 – archae0pteryx

+0

@ archae0pteryx,我一不留神,请编辑 – Andrew

1

在这里你去,只使用纯JS:

const raw = { 
    "-Kdkahgiencls0dnh": { 
    "name": "a name", 
    "desc": "a description", 
    "other": "some other guff" 
    }, 
    "-Ksdfadfvcls0dsnh": { 
    "name": "another name", 
    "desc": "another description", 
    "other": "some more" 
    }, 
    "-kgoandiencls0dnh": { 
    "name": "I am a name", 
    "desc": "I am a description", 
    "other": "I am some other guff" 
    } 
} 

let formatted = Object.keys(raw).map(
    key=>Object.assign(raw[key], {"id": ""+key}) 
); 

这里是一个fiddle获得现场演示。

3

Lodash的_.map()回调接收作为第二个参数的迭代键。使用对象赋值,创建具有键ID的新对象:

const array = _.map(data, (item, id) => Object.assign({ id }, item)) 

演示:

const data = {"-Kdkahgiencls0dnh":{"name":"a name","desc":"a description","other":"some other guff"},"-Ksdfadfvcls0dsnh":{"name":"another name","desc":"another description","other":"some more"},"-kgoandiencls0dnh":{"name":"I am a name","desc":"I am a description","other":"I am some other guff"}}; 
 

 
const array = _.map(data, (item, id) => Object.assign({ id }, item)); 
 

 
console.log(array);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

+1

个人。我不喜欢lodash的'map'风格如何处理一个集合并输出一个数组,但是这种行为正是OP所期待的。 – aaaaaa