有谁知道如何在 Ionic Storage 中合并 2 个阵列?
Does anyone have an idea about how to merge 2 arrays in Ionic Storage?
我在两步警报中获取用户输入并单独保存数据。我想合并像 "www.youtube.com/"+"watch?v=tAGnKpE4NCI" = "www.youtube.com/watch?v=tAGnKpE4NCI"
这样的数组。
我查找并找到 concat()
但它是这样工作的:
A = [1,2,3]
B = [a,b,c]
C = A.concat( B )
C = [1,2,3,a,b,c]
我想实现这个:C = [1,a,2,b,3,c]
这些是模拟器截图:
type: Array<{type: any}> = [];
myEncodedData: Array<{}> = [];
extention: Array<{extention: any}> = [];
testRadioOpen: boolean;
testRadioResult: any;
async generateQR(type) {
const alert = await this.alertController.create({
header: 'Create your personalize QR!',
inputs: [
{
name: 'insta',
type: 'radio',
label: 'www.instagram.com/',
value: 'www.instagram.com/',
checked: type === 'insta'
},
{
name: 'face',
type: 'radio',
value: 'www.facebook.com/',
label: 'www.facebook.com/',
checked: type === 'face'
},
{
name: 'youtube',
type: 'radio',
value: 'www.youtube.com/',
label: 'www.youtube.com/',
checked: type === 'youtube'
},
{
name: 'twitter',
type: 'radio',
value: 'www.twitter.com/',
label: 'www.twitter.com/',
checked: type === 'twitter'
},
{
name: 'empty',
type: 'radio',
value: '',
label: '',
checked: true
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
}
}, {
text: 'Ok',
handler: async (data) => {
const alert = await this.alertController.create({
header: 'Create your personalize QR!',
inputs: [
{
name: 'extention',
type: 'text',
placeholder: 'enter an extention: '
},
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
}
}, {
text: 'Ok',
handler: (data) => {
console.log('Radio data:', data);
this.testRadioOpen = false;
this.testRadioResult = data;
this.type.push({
type: this.testRadioResult,
});
}
}
]
});
await alert.present();
this.extention = data;
this.extention.push({
extention: this.testRadioResult,
});
this.storage.set('extention', this.extention);
this.barcodeScanner.encode(this.barcodeScanner.Encode.TEXT_TYPE, this.extention).then((ReplaceSource) => {
console.log(ReplaceSource);
this.myEncodedData = ReplaceSource;
});
}
}
]
});
await alert.present();
}
我不确定你是否想要数组作为
"www.youtube.com/"+"watch?v=tAGnKpE4NCI" = ["www.youtube.com/watch?v=tAGnKpE4NCI"]
正如您在示例中设置的那样
"www.youtube.com/"+"watch?v=tAGnKpE4NCI" = ["www.youtube.com/","watch?v=tAGnKpE4NCI"]
但对于第一个,您可以使用 .map 来实现。
映射 a,然后在每个循环中使用索引来获取 b 的元素。
const a = ['you', 'insta', 'c', 'd'];
const b = ['loc1', 'loc2', 'loc3', 'loc4'];
var c = a.map(function (d, i) {
return d + String(b[i])
})
console.log(c)
// ["youloc1", "instaloc2", "cloc3", "dloc4"]
我在两步警报中获取用户输入并单独保存数据。我想合并像 "www.youtube.com/"+"watch?v=tAGnKpE4NCI" = "www.youtube.com/watch?v=tAGnKpE4NCI"
这样的数组。
我查找并找到 concat()
但它是这样工作的:
A = [1,2,3]
B = [a,b,c]
C = A.concat( B )
C = [1,2,3,a,b,c]
我想实现这个:C = [1,a,2,b,3,c]
这些是模拟器截图:
type: Array<{type: any}> = [];
myEncodedData: Array<{}> = [];
extention: Array<{extention: any}> = [];
testRadioOpen: boolean;
testRadioResult: any;
async generateQR(type) {
const alert = await this.alertController.create({
header: 'Create your personalize QR!',
inputs: [
{
name: 'insta',
type: 'radio',
label: 'www.instagram.com/',
value: 'www.instagram.com/',
checked: type === 'insta'
},
{
name: 'face',
type: 'radio',
value: 'www.facebook.com/',
label: 'www.facebook.com/',
checked: type === 'face'
},
{
name: 'youtube',
type: 'radio',
value: 'www.youtube.com/',
label: 'www.youtube.com/',
checked: type === 'youtube'
},
{
name: 'twitter',
type: 'radio',
value: 'www.twitter.com/',
label: 'www.twitter.com/',
checked: type === 'twitter'
},
{
name: 'empty',
type: 'radio',
value: '',
label: '',
checked: true
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
}
}, {
text: 'Ok',
handler: async (data) => {
const alert = await this.alertController.create({
header: 'Create your personalize QR!',
inputs: [
{
name: 'extention',
type: 'text',
placeholder: 'enter an extention: '
},
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
}
}, {
text: 'Ok',
handler: (data) => {
console.log('Radio data:', data);
this.testRadioOpen = false;
this.testRadioResult = data;
this.type.push({
type: this.testRadioResult,
});
}
}
]
});
await alert.present();
this.extention = data;
this.extention.push({
extention: this.testRadioResult,
});
this.storage.set('extention', this.extention);
this.barcodeScanner.encode(this.barcodeScanner.Encode.TEXT_TYPE, this.extention).then((ReplaceSource) => {
console.log(ReplaceSource);
this.myEncodedData = ReplaceSource;
});
}
}
]
});
await alert.present();
}
我不确定你是否想要数组作为 "www.youtube.com/"+"watch?v=tAGnKpE4NCI" = ["www.youtube.com/watch?v=tAGnKpE4NCI"] 正如您在示例中设置的那样 "www.youtube.com/"+"watch?v=tAGnKpE4NCI" = ["www.youtube.com/","watch?v=tAGnKpE4NCI"]
但对于第一个,您可以使用 .map 来实现。 映射 a,然后在每个循环中使用索引来获取 b 的元素。
const a = ['you', 'insta', 'c', 'd'];
const b = ['loc1', 'loc2', 'loc3', 'loc4'];
var c = a.map(function (d, i) {
return d + String(b[i])
})
console.log(c)
// ["youloc1", "instaloc2", "cloc3", "dloc4"]