如何从执行条件的数组中获取所有值?

时间:2019-12-15 22:38:13

标签: javascript

我有这样的数组,我想获取所有值,例如,描述等于给定,并且我知道如何在执行条件的情况下获取第一个数组值,但是我不知道如何从描述等于给定的所有条件中获取。

 [
  Discovery {
    name: 'Lorem ipsum',
    description: 'lorem ipsum',
    date: 2019-12-15T22:03:17.078Z,
    url: 'https://loremipsum.io/',
    discoveryId: 1,
    userId: 1
  },
  Discovery {
    name: 'Lorem ipsum2',
    description: 'bbbbbbb',
    date: 2019-12-15T22:03:17.079Z,
    url: 'https://loremipsum.io/',
    discoveryId: 2,
    userId: 2
  },
  Discovery {
    name: 'Lorem ipsum3',
    description: 'bbbbbbb',
    date: 2019-12-15T22:03:17.079Z,
    url: 'https://loremipsum.io/',
    discoveryId: 3,
    userId: 3
  }
]

这是数组的来源

let nextId;
const discoveryList = [];
class Discovery{
    constructor(name, description, date, url, discoveryId, userId){
        this.name = name;
        this.description = description;
        this.date = date;
        this.url = url;
        this.discoveryId = discoveryId;
        this.userId = userId;
    }

    static addDiscovery(discovery, userId){
        discovery.discoveryId = nextId++;
        discovery.userId = userId;
        discoveryList.push(discovery);

        return discovery;
    }

static discoveryList(){
        return discoveryList;
    }
}

1 个答案:

答案 0 :(得分:3)

您可以根据自己的情况filter阵列。例如:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart';


class Loading extends StatefulWidget {
  @override
  _LoadingState createState() => _LoadingState();
}

class _LoadingState extends State<Loading> {

  void getTime() async {
   Response response= await get('http://worldtimeapi.org/timezone/Europe/London');
    Map data=jsonDecode(response.body);
    print(data);


  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getTime();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(
        backgroundColor: Colors.blue[800],
        title: Text('here is loading'),
      ),
      body: Text("choose location screen"),
    );
  }
}