如何从嵌套JSON数组访问所有值?

时间:2018-08-15 16:21:59

标签: arrays json object react-native jsx

这是我的JSON数组:

    [{"id":"11",
"first_name":"John",
"last_name":"Doe",
"username":"Username1234",
"email":"example1426@gmail.com",
"who":"Person",
"user_images":[{"id":"114",
"username":"Hannah_L123",
"images":"resized_1522-20131219-creativity.jpg","date":"12\/04\/2017"}]}]

我不使用第一个数组,但是我确实使用第二个数组:

  user_image:  responseJson[0].user_images.map(item => ({ ...item, progress: new Animated.Value(0), unprogress: new Animated.Value(1) }))

我只想访问username中已映射的所有user_images。尝试以下操作不起作用:{this.state.user_image.username} //未定义{this.state.user_image["username"]} //未定义

我在做什么错了?

编辑:我发现了如何做:this.state.user_image[0].username,但是我如何console.log() username包含fetch('https://www.example.com/React/user.php') .then((response) => response.json()) .then((responseJson) => { this.setState({ user_image: responseJson[0].user_images.map(item => ({ ...item, progress: new Animated.Value(0), unprogress: new Animated.Value(1) })), }, function() { const userImagesWithUsername = this.state.user_image.map(item => item.username != undefined); console.log(userImagesWithUsername); }); }) .catch((error) => { //console.error(error); }); 的所有数据呢?我只从控制台登录3。

反应本机代码:

Public Sub ColourLastThree(Target As Range, Optional CharCount As Long = 3, Optional RGBColour As Long = 255)

    Dim rCell As Range

    For Each rCell In Target
        If Len(rCell) >= CharCount Then
            rCell = "'" & rCell
            rCell.Characters(Start:=Len(rCell) - (CharCount - 1), Length:=CharCount).Font.Color = RGBColour
        End If
    Next rCell

End Sub

1 个答案:

答案 0 :(得分:1)

要获取包含username的所有数据,可以过滤user_image数组。

const userImagesWithUsername = this.state.user_image.filter(item => item.username != undefined);