通过键的两个JSON之间的关系

时间:2018-12-13 15:13:54

标签: json vue.js key relativelayout

所以我有2种不同的JSON。 图书

{
  "books": [{
    "name": "Lord of the rings",
    "author": 1,
    "year": 1937,
    "genre": 3,
    "imageUrl": "https://cdn.lifehack.org/wp-content/uploads/2015/03/9780618640157_custom-s6-c30.jpg",
    "availability": true
  }]
}

,另一个是作者

{
  "authors": [{
    "id": 1,
    "name": "J.R.R. Tolkien"
  }, {
    "id": 2,
    "name": "Harper Lee"
  }, {
    "id": 3,
    "name": "J.K. Rowling"
  }]
}

我想让作者喜欢一个id,所以当我显示一本书的作者时,我想要的是名称(在第二个json中),而不是id。 我怎样才能做到这一点? 不确定是否重要,但我正在VUE工作。

3 个答案:

答案 0 :(得分:0)

您可以使用loash的find方法执行此操作,例如

文档:https://lodash.com/docs/4.17.11#find

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

/* dynamic memory storage - start */
typedef struct {
    size_t m_size;
    size_t m_reserved;
    int* m_data;
} store;

/* create a store */
store* store_create() {
    store* s = malloc(sizeof(store));
    if(s) {
        /* initialize values */
        s->m_size = 0;
        s->m_reserved = 1; /* a very conservative start value */
        s->m_data = malloc(sizeof(int)*s->m_reserved);
        if(s->m_data==NULL) {
            free(s);
            s = NULL;
        }
    }
    return s;
}

/* destroy a store */
void store_destroy(store* s) {
    /* free the integer array */
    free(s->m_data);
    /* free the store struct */
    free(s);
}

bool store_reserve(store* s, size_t new_res) {
    /* reserve more space for the int's if needed */
    if(new_res>s->m_reserved) {
        int* n = realloc(s->m_data, sizeof(int)*new_res);
        if(n==NULL) return false; /* could not expand storage */
        s->m_reserved = new_res;
        s->m_data = n;
    }
    return true;
}

// check if it's time to increase reserved storage
bool store_check_reserve(store* s) {
    if(s->m_size == s->m_reserved)
        /* change 5/4 to a larger value for more aggressive
         * increase of memory allocation */
        return store_reserve(s, (s->m_reserved+1)*5/4);
    else
        return true;
}

// add a value to the store
bool store_add(store* s, int v) {
    if(!store_check_reserve(s)) return false;

    s->m_data[s->m_size++] = v;
    return true;
}

/* dynamic memory storage - end */

int main(int argc, char* argv[]) {
    FILE* fp;
    fp = fopen("file.txt", "r");

    /* create a store for our unknown amount of int's */
    store* myStore = store_create();

    /* scan for strings separated by comma and newline
     * and allocate memory for it */
    char* str;
    while(fscanf(fp, " %m[^,\n],", &str)==1)
    {
        int num;
        /* convert string to integer */
        if(sscanf(str, "%d", &num)==1) {
            /* store the extracted value */
            if(store_add(myStore, num)==false) {
                fprintf(stderr, "FAILED STORING %d\n", num);
            }
        }
        // free string allocated by fscanf (%m) */
        free(str);
    }
    fclose(fp);

    printf("All %d stored values:\n", myStore->m_size);
    for(size_t i=0; i<myStore->m_size; ++i) {
        printf("%d = %d\n", i, myStore->m_data[i]);
    }

    /* release memory allocated by our store */
    store_destroy(myStore);
    return 0;
}

答案 1 :(得分:0)

我将使用本机Array.filter方法,如下所示:

let tolkien = authors.filter(author => !!author.id === 1)

使用.filter意味着您不需要为此方法包括像lodash这样的库。

答案 2 :(得分:0)

您可以使用映射到作者的键(即author[1] = {id: 1, ...})创建一个新对象,这将使您可以快速,轻松地按其ID访问任何作者。示例:

let authorByIds = {};
authors.forEach(author => {
    authorByIds[author.id] = {...author};
});

let firstBook = books[0];

console.log(authorByIds[firstBook.author]);
// will output author with id = 1, i.e J.R.R. Tolkien