Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从这个嵌套数组中获取值_Javascript_Arrays - Fatal编程技术网

Javascript 如何从这个嵌套数组中获取值

Javascript 如何从这个嵌套数组中获取值,javascript,arrays,Javascript,Arrays,我想迭代这个对象数组,但在尝试映射它时会不断出错。它是使用npm软件包“react select”中的multi-select生成的。在React中使用javascript时正在寻找javascript答案 我尝试过的例子是,我现在只是尝试将值获取到console.log。当我运行它时,我得到TypeError:无法读取未定义的属性“map” 我相信我记得在过去遇到过这样一个问题:数组中的对象前面有一个数字,但我不知道这种类型的数组调用什么来诊断这个问题 this.state.typeOfFru

我想迭代这个对象数组,但在尝试映射它时会不断出错。它是使用npm软件包“react select”中的multi-select生成的。在React中使用javascript时正在寻找javascript答案

我尝试过的例子是,我现在只是尝试将值获取到console.log。当我运行它时,我得到TypeError:无法读取未定义的属性“map”

我相信我记得在过去遇到过这样一个问题:数组中的对象前面有一个数字,但我不知道这种类型的数组调用什么来诊断这个问题

this.state.typeOfFruit = [
   0: {label: "label 1", value: "apple"}
   1: {label: "label 2", value: "orange"}
   2: {label: "label 3", value: "banana"} 
]

typeOfFruit.map((fruit) => console.log(fruit.value))
我的预期结果是[苹果、橘子、香蕉]

****编辑****

我将包含创建错误数组的react-select multi-select表单的代码。因此,提交后的信息将通过我的redux存储进入Firebase Firestore。最终结果是上面的数组格式不正确。这是一个更大的代码库,所以我尝试发布所有应该应用于该特定代码路径的内容

class FruitOrderForm extends Component {
   constructor(props) {
       super(props)
       this.state = {
           typeOfFuit: [],
       }
   }

const fruitOptions = [
   { value: 'apple', label: 'label 1' },
   { value: 'orange', label: 'label 2' },
   { value: 'banana', label: 'label 3' },
]

    handleMultiSelectChange = (typeOfFruit) => {
       this.setState({ typeOfFruit });
  }

    onSubmit = (e) => {
       e.preventDefault();
       this.props.updateProfile(this.state)
       document.getElementById("fruitOrderForm").reset();
}

render() {
   typeOfFruit.map((fruit) => console.log(fruit.value))
   return (
      <div>
         <form id='fruitOrderForm' onSubmit={this.onSubmit}>
            < Select
               id='typeOfFruit'
               value={this.state.typeOfFruit}
               onChange={this.handleMultiSelectChange}
               options={fruitOptions }
               isMulti='true'
               isSearchable='true'
            />
            <button>Update</button>
         </form>
     </div>
  )
}
}

const mapStateToProps = (state, ownProps) => {
    const profile = state.firebase.profile

    return{
       profile: profile,
       auth: state.firebase.auth
    } 
}

const mapDispatchToProps = (dispatch) => {
    return {
        updateProfile: (users) => dispatch(updateProfile(users)),
    }
}

export default compose(
    connect(mapStateToProps, mapDispatchToProps),
    firestoreConnect([{ collection: 'users'}]))(
FruitOrderForm)
然后它通过一个减速器

const businessProfileReducter = (state = initState, action) => {
   switch (action.type) {
      case 'CREATE_BUSINESS_PROFILE': 
        console.log('created business profile', action.businessProfile)
        return state;
      case 'CREATE_BUSINES_PROFILE_ERROR': 
        console.log('create business profile error', action.err);
        return state;
      default:
        return state;
   }
}

export default businessProfileReducter;
然后是根部减压器

const rootReducer = combineReducers({
   auth: authReducer,
   businessProfile: businessProfileReducer,
   firestore: firestoreReducer,
   firebase: firebaseReducer
})

export default rootReducer

我认为这应该涵盖一切。如果有人看到任何明显的漏洞,请告诉我。

看起来数组的定义方式有语法错误,数组不应该包含键值对。 请检查下面的代码片段。 希望能有帮助

typeofruit=[
{label:“label 1”,值:“apple”},
{标签:“标签2”,值:“橙色”},
{标签:“标签3”,值:“香蕉”}
]
typeofruit.map((fruit)=>console.log(fruit.value))
typeofruit=[
{label:“label 1”,值:“apple”},
{标签:“标签2”,值:“橙色”},
{标签:“标签3”,值:“香蕉”}
]
var newArray=typeofruit.map(x=>{
返回x.value
});

log(newArray)您的数组格式不正确,是的,但有两种可能的修复方法

对象数组 一个更干净的是:

const typeofruit=[
{label:“label 1”,值:“apple”},
{标签:“标签2”,值:“橙色”},
{标签:“标签3”,值:“香蕉”}
]

typeofruit.map((fruit)=>console.log(fruit.value))
您是否在
严格模式下运行代码?请使用
let,const
创建一个适当的变量,然后尝试访问数组元素,您可能还希望在遍历数组之前移动数组。此外,您不需要勾号(除非它们实际上在代码中,这对您没有帮助!)。缩进就行了。对内联代码(如
this
)使用勾号,而不是块。欢迎您,狩猎愉快。@Hassaniam,我没有使用严格模式。至于let或const,我是在react中这样做的,所以实际上应该是this.state.typeofruit,我只是把它省略了,因为我觉得这对我的问题无关紧要。我把它加进去了,以防它不像我想的那样无关紧要。此外,我随后正在访问数组,因为它已设置为我的状态。@D\N.谢谢。第一次张贴。我很欣赏你对背景的洞察力。是的。我同意它不应该看起来像我拥有的,但它确实如此。这正是我面临的问题。我相信有一种方法可以去除物体之前的数字,但不确定如何去除。我不需要数字。我希望数字不在那里,因为这就是我的问题所在。可能我需要重新定义我的问题,因为react select正在发送格式错误的数组。我将编辑我的问题,以包括创建坏数组的react select multi-select部分。这是一个完全不同的问题。祝你好运,但最好是为这一部分提出一个全新的问题。
const rootReducer = combineReducers({
   auth: authReducer,
   businessProfile: businessProfileReducer,
   firestore: firestoreReducer,
   firebase: firebaseReducer
})

export default rootReducer