看下面的代码:
var exemples = [
{
'name' : 'd',
'index' : 3
},
{
'name' : 'c',
'index' : 2
},
{
'name' : 'a',
'index' : 0
},
{
'name' : 'b',
'index' : 1
}
];
const list = exemples.map((exemple, index, array) => exemple.name)
console.log(list)Run Code Online (Sandbox Code Playgroud)
它给了我那个数组:
["d", "c", "a", "b"]
Run Code Online (Sandbox Code Playgroud)
我想尊重索引并得到这样的结果:
["a", "b", "c", "d"]
Run Code Online (Sandbox Code Playgroud)
听起来像是一个基本问题,但我需要你的帮助。谢谢。
我在编码方面很新。我正在研究 ReactJS,我试图在列表中显示来自 Firebase 的数据库中的所有不同类别。你可以在我写的代码下面看到:
import Link from 'next/link'
import React, { Component } from 'react'
import firebase from 'firebase'
export default class extends Page {
constructor () {
super()
this.state = {
datas: ''
}
}
componentDidMount () {
firebase.initializeApp(clientCredentials)
// ------- Connect to the firebase DATABASE -------- ////
firebase.database().ref().orderByChild("sectionIndex")
.on("child_added",
snapshot => {this.setState({datas: snapshot.val().category});},
error => {console.log("Error: " + error.code);});
}
render () {
return (
<ul>
{this.state.datas.map((data, i) =>
<li key={i}> {data.category} </li>
</ul>
)
}
}Run Code Online (Sandbox Code Playgroud)
它说 …
javascript arrays firebase reactjs firebase-realtime-database