访问数组的单个元素的最佳做法

时间:2019-04-05 15:01:21

标签: javascript filter

从javascript对象中过滤单个元素的最佳方法是什么

data.filter(({id}) => {return id === id})

我得到一个元素数组,并且必须对其进行索引才能访问元素。有什么get方法可以更好地处理这种情况?

2 个答案:

答案 0 :(得分:2)

您应该使用Array.find

来自MDN:

  

find()方法返回满足提供的测试功能的数组中第一个元素的值。否则返回undefined。

示例:

#include <stdio.h>
#include <stdlib.h>
/* The graph vertices are labeled as 1,2,3,4,5 but represented as 0,1,2,3,4 */
typedef struct ListNode{
    int vertex_number;
    int weight;
    struct ListNode* next;
}Node;

typedef struct Graph {
    int no_vertices;
    int source;
    int total_weight;
    Node* list;

}Graph;

Graph* initialize_graph(FILE *fnodes, Graph *graph) {
    int total_vertex, source;
    fscanf(fnodes, "%d,%d", &total_vertex, &source);
    graph -> no_vertices = total_vertex;
    graph -> source = source;
    graph -> list = (Node*)malloc(sizeof(Node) * (graph->no_vertices));

    return graph;
}

Node* adjacency_list(FILE *fnodes, Graph * graph) {
    int total_weight=0, start, end, weight;
    Node *temp;
    Node *trav = graph -> list;
    // Preprocessing of the adjacency list
    for (int i = 0; i < graph->no_vertices; ++i)
    {
        graph -> list[i].vertex_number = i; // to represent each vertex
        graph -> list[i].next = NULL;
    }


    while(fscanf(fnodes, "%d,%d,%d", &start, &end, &weight) == 3) {
        temp = (Node *)malloc(sizeof(Node));
        temp -> vertex_number = end;
        temp -> next = NULL;
        temp -> weight = weight;
        trav  = &(graph -> list[start - 1]);
        while(trav -> next != NULL) {
            trav = trav -> next;
        }
        trav -> next = temp;
        total_weight += weight;
    }
    graph -> total_weight = total_weight;
    return graph -> list;
}

void print(Graph *graph) {
    Node* trav = graph -> list;
    for (int i = 0; i < graph -> no_vertices; ++i)
    {
        trav = &(graph -> list[i]);
        printf("Vertex no. : %d ", graph -> list[i].vertex_number);
        while (trav -> next != NULL) {
            printf("DEST : %d \n", trav -> next -> vertex_number);
            trav = trav -> next;
        }
    }
    printf("\n");
}


int main() {
    FILE *fnodes = fopen("nodes.txt", "r");
    Graph *graph;
    graph = initialize_graph(fnodes, graph);
    printf("Details of graph, Nodes and Source: %d %d\n", graph->no_vertices, graph->source);
    printf("--Adjacency List--(Started from Vertex number 0)\n");
    graph -> list = adjacency_list(fnodes, graph);
    printf("Total Weight: %d\n", graph -> total_weight);
    print(graph);
    int distance_array_new[graph -> no_vertices]; //failed
    int *distance_array;
    distance_array = (int *)malloc(sizeof(int) * (graph -> no_vertices));
     if (distance_array == NULL) {
        printf("NO MEMORY\n");
     }
    /* NOTE: If I try to initialize the array normally, or read it's contents in a for loop,
    or try to change the value, check the pointer it gives a Seg fault and I don't know Why.
    Uncomment below code and Run */
    printf("V: %d\n", distance_array[0]);

     for (int i = 0; i < (graph -> no_vertices); i++)
     {
        distance_array[i] = (graph -> total_weight) + 15;
    }
     distance_array[(graph -> source) - 1] = 0; //Initial distance is 0 for source.
}

答案 1 :(得分:1)

您应为对象的id属性使用其他名称

data.filter(({id:id2}) => {return id === id2})

您还可以删除{}return来缩短代码

data.filter(({id:id2}) => id === id2)

在这种情况下,我不会破坏对象

data.filter(o => o.id === id)