我想创建一个像这样的对象:
"store1":
{
"isbn":"3129321903",
"title":"here comes the title",
"author":"author of the book"
},
{
"isbn":"3333333333",
"title":"title of second book",
"author":"author of second book"
}
"store2":
{
"isbn":"3333311111",
"title":"title of book from store2",
"author":"author of book from store2"
}
我试过这样的事情:
var storeArray = [];
for(var i = 0; i < response.data.bookInfo.length; i++){
var keys = Object.keys(response.data.bookInfo[i]);
var storename = keys.filter(key => key != 'isbn' && key != 'metadata'); // get storename
if(storeArray[storename] == undefined){
storeArray[storename] = response.data.bookInfo[i];
}
else{
storeArray[storename].push(response.data.bookInfo[i]);
}
}
但是这只会给我带来以下错误:
TypeError:storeArray [“amazon”]。push不是函数
当我使用storeArray[storename] = response.data.bookInfo[i]
时,它只显示我在控制台中的一行,我想将响应数据添加到storeArray [storename]以获取商店的书籍列表。
答案 0 :(得分:3)
这意味着您的public class BluetoothServices extends Service {
private BluetoothAdapter mBluetoothAdapter;
public static final String B_DEVICE = "MY DEVICE";
public static final String B_UUID = "00001101-0000-1000-8000-00805f9b34fb";
// 00000000-0000-1000-8000-00805f9b34fb
public static final int STATE_NONE = 0;
public static final int STATE_LISTEN = 1;
public static final int STATE_CONNECTING = 2;
public static final int STATE_CONNECTED = 3;
private ConnectBtThread mConnectThread;
private static ConnectedBtThread mConnectedThread;
private static Handler mHandler = null;
public static int mState = STATE_NONE;
public static String deviceName;
public static BluetoothDevice sDevice = null;
public Vector<Byte> packData = new Vector<>(2048);
//IBinder mIBinder = new LocalBinder();
@Nullable
@Override
public IBinder onBind(Intent intent) {
//mHandler = getApplication().getHandler();
return mBinder;
}
public void toast(String mess){
Toast.makeText(this,mess,Toast.LENGTH_SHORT).show();
}
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
BluetoothServices getService() {
// Return this instance of LocalService so clients can call public methods
return BluetoothServices.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String deviceg = intent.getStringExtra("bluetooth_device");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
connectToDevice(deviceg);
return START_STICKY;
}
private synchronized void connectToDevice(String macAddress){
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(macAddress);
if (mState == STATE_CONNECTING){
if (mConnectThread != null){
mConnectThread.cancel();
mConnectThread = null;
}
}
if (mConnectedThread != null){
mConnectedThread.cancel();
mConnectedThread = null;
}
mConnectThread = new ConnectBtThread(device);
toast("connecting");
mConnectThread.start();
setState(STATE_CONNECTING);
}
private void setState(int state){
mState = state;
if (mHandler != null){
// mHandler.obtainMessage();
}
}
public synchronized void stop(){
setState(STATE_NONE);
if (mConnectThread != null){
mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null){
mConnectedThread.cancel();
mConnectedThread = null;
}
if (mBluetoothAdapter != null){
mBluetoothAdapter.cancelDiscovery();
}
stopSelf();
}
public void sendData(String message){
if (mConnectedThread!= null){
mConnectedThread.write(message.getBytes());
toast("sent data");
}else {
Toast.makeText(BluetoothServices.this,"Failed to send data",Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean stopService(Intent name) {
setState(STATE_NONE);
if (mConnectThread != null){
mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null){
mConnectedThread.cancel();
mConnectedThread = null;
}
mBluetoothAdapter.cancelDiscovery();
return super.stopService(name);
}
/*private synchronized void connected(BluetoothSocket mmSocket){
if (mConnectThread != null){
mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null){
mConnectedThread.cancel();
mConnectedThread = null;
}
mConnectedThread = new ConnectedBtThread(mmSocket);
mConnectedThread.start();
setState(STATE_CONNECTED);
}*/
private class ConnectBtThread extends Thread{
private final BluetoothSocket mSocket;
private final BluetoothDevice mDevice;
public ConnectBtThread(BluetoothDevice device){
mDevice = device;
BluetoothSocket socket = null;
try {
socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString(B_UUID));
} catch (IOException e) {
e.printStackTrace();
}
mSocket = socket;
}
@Override
public void run() {
mBluetoothAdapter.cancelDiscovery();
try {
mSocket.connect();
Log.d("service","connect thread run method (connected)");
SharedPreferences pre = getSharedPreferences("BT_NAME",0);
pre.edit().putString("bluetooth_connected",mDevice.getName()).apply();
} catch (IOException e) {
try {
mSocket.close();
Log.d("service","connect thread run method ( close function)");
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
//connected(mSocket);
mConnectedThread = new ConnectedBtThread(mSocket);
mConnectedThread.start();
}
public void cancel(){
try {
mSocket.close();
Log.d("service","connect thread cancel method");
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class ConnectedBtThread extends Thread{
private final BluetoothSocket cSocket;
private final InputStream inS;
private final OutputStream outS;
private byte[] buffer;
public ConnectedBtThread(BluetoothSocket socket){
cSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
tmpOut = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
inS = tmpIn;
outS = tmpOut;
}
@Override
public void run() {
buffer = new byte[1024];
int mByte;
try {
mByte= inS.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
Log.d("service","connected thread run method");
}
public void write(byte[] buff){
try {
outS.write(buff);
} catch (IOException e) {
e.printStackTrace();
}
}
private void cancel(){
try {
cSocket.close();
Log.d("service","connected thread cancel method");
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onDestroy() {
this.stop();
super.onDestroy();
}
不是数组。
在
storeArray[storename]
您只需为storeArray[storename] = response.data.bookInfo[i];
分配一个不是数组的值。首先需要创建一个数组,然后将该元素放入其中。
storeArray[storename]
在下一次迭代中,您将拥有一个包含一个元素的数组,并且可以使用storeArray[storename] = [ response.data.bookInfo[i] ];
。
答案 1 :(得分:1)
push
是Array
本身的函数。所以:storeArray.push(...)