1

我有两张桌子:

1 - 用户

用户名
1 名称1 最后1
2 名称2 最后2

2 - 书籍

身份证 姓名簿 用户名
1 书1 1
2 书2 1
3 书3 2

我正在使用React-Nativereact-native-sqlite-storage

listofBooks() {
    return new Promise(resolve => {
      var books= [];    
      this.initDB()
        .then(db => {
          db.transaction(tx => {
            tx.executeSql('SELECT * from Users INNER JOIN Books ON Users.IdUser= Books.IsUser',
            ).then(([tx, results]) => {
              var len = results.rows.length;
              if (len > 0) {
                for (let i = 0; i < len; i++) {
                  let row = results.rows.item(i);
                  const { IdUser, FirstName, LastName, NameBook } = row;
                  books.push({ IdUser, FirstName, LastName, NameBook  });
                }
                resolve(JSON.stringify(books));
              }
            });
          })
            .then(result => {
              this.closeDatabase(db);
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(err => {
          console.log(err);
        });
    });
  }

函数返回这个 json

[
  {
    "IdUser": 1,
    "FirstName": "name1",
    "LastName": "last1",
    "NameBook": "book1",
  },
  {
    "IdUser": 1,
    "FirstName": "name1",
    "LastName": "last1",
    "NameBook": "book2",
  },
  {
    "IdUser": 2,
    "FirstName": "name2",
    "LastName": "last2",
    "NameBook": "book3",
  }
]

但我希望我的 json 是那样的

[
  {
    "IdUser": 1,
    "FirstName": "name1",
    "LastName": "last1",
    "NameBook": ["book1", "book2"],
  },
  {
    "IdUser": 2,
    "FirstName": "name2",
    "LastName": "last2",
    "NameBook": "book3",
  }
]

是否有(react-native)库可以让我轻松解决这个问题?感谢帮助。

4

0 回答 0