我想将一个数组的元素插入另一个数组。我有两个名为a和b的数组,我想从数组a中获取订单数量并将其插入到数组b的对象中:这是数组
var a = [{
"order_id": "241918",
"product_id": "152737",
"order_qty": "1",
"customer_note": "Only nappy pants subsitutes please",
"out_of_stock": "find_best_match",
"cust_sub_prod_id": null,
"cust_sub_prod_qty": null
}]
var b = [{
"id": "282",
"product_id": "152737",
"sku": "b175a9ea5f4d9b4766e74079c2bec8",
"prod_name": "PnP Baby Marrows 1kg",
"image_url": "https://www.onecart.co.za/wp-content/uploads/2017/11/108890_EA.jpg",
"vendor": "Pick n Pay",
"price": "40.69"
}]
答案 0 :(得分:0)
都是包含一个普通对象的一元数组 因此,使用b [0]可以访问fisrt和唯一的元素(对象) 和.order_qty允许您访问所需的属性。
b[0].order_qty = a[0].order_qty;
答案 1 :(得分:0)
我不确定这样做是否正确,但是您可以使用var mysql = require("mysql");
var httpSyncRequest = require('sync-request');
var MysqlDatabase = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "myDB"
});
MysqlDatabase.connect();
setInterval(function () {
MysqlDatabase.query("SELECT 1", function (err, row) {
});
}, 3000);
MysqlDatabase.query(`Select * from myTable`,function(err,row){
row.forEach(function (result) {
var isOpening= httpSyncRequest("GET",
"https://myapi.com?ID="+result.ID
).getBody('utf8');
MysqlDatabase.query(`UPDATE myTable
SET IsOpening =?
WHERE
myTable.ID= ?`,[isOpening,result.ID],function(){});
});
});
来实现与数据相同的代码,如下所示:
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const devMode = process.env.NODE_ENV === 'development';
module.exports = {
entry: {
vendor1: ['vendor1', 'vendor2', 'vendor 3'], // all other vendor lists, package.json dependencies
vendor2: ['vue'],
app: './assets/src/app.js'
},
resolve: {
alias: {
'~': path.resolve(__dirname, 'assets/src')
}
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
'css-loader',
'postcss-loader',
'sass-loader'
]
},
{
test: /\.css$/,
use: ['vue-style-loader', 'css-loader']
},
{
test: /\.(woff2?|ttf|eot|svg)$/,
use: ['url-loader']
}
]
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendor1: {
name: 'vendor1',
test: 'vendor1',
enforce: true
},
vendor2: {
name: 'vendor2',
test: 'vendor2',
enforce: true
}
}
}
},
externals: {
jquery: 'jQuery'
},
plugins: [new VueLoaderPlugin()]
};
答案 2 :(得分:0)
1。如果不确定数组B是否具有与product_id匹配的对象
a.forEach((from)=>{
//search is there any matching object in array B
let to = b.find(searching=>from.product_id === searching.product_id);
if(to!=null){
to.order_qty = from.order_qty
}
});
2。如果您确定数组B具有与product_id匹配的对象
a.forEach((from)=>b.find(searching=>from.product_id === searching.product_id).order_qty = from.order_qty);
哦,看来您最好使用其他数据结构而不是数组来存储数据。
好吧,为什么?
Array.prototype.find()
的复杂度为O(n),这意味着当数组的长度为1000时,最坏的情况将导致数组内部的find()
循环1000次。
当数组A和B都具有n个对象时,使用上述解决方案将使数组B循环n ^ 2次。
如何解决?
大多数JavaScript引擎使用哈希表实现Map,因此
Map.prototype.get()
的复杂度为O(1),无论您的Map有1k,10k,100k个对象,都使用其键搜索对象只需要1次。
//Creating data
let a = new Map();
let b = new Map();
a.set("152737",{
"order_id": "241918",
"product_id": "152737",
"order_qty": "1",
"customer_note": "Only nappy pants subsitutes please",
"out_of_stock": "find_best_match",
"cust_sub_prod_id": null,
"cust_sub_prod_qty": null
});
b.set("152737",{
"id": "282",
"product_id": "152737",
"sku": "b175a9ea5f4d9b4766e74079c2bec8",
"prod_name": "PnP Baby Marrows 1kg",
"image_url": "https://www.onecart.co.za/wp-content/uploads/2017/11/108890_EA.jpg",
"vendor": "Pick n Pay",
"price": "40.69"
})
//Search and insert data
a.forEach(from =>{
if(b.has(from.product_id)){
b.get(from.product_id).order_qty = from.order_qty
}
})