Create a dynamic SQL Insert from array object - arrays

I have a project in Typescript where I need to create an Insert with all the elements of the object to make only one Insert, instead of one Insert for each array.
This is my current function:
public async insert() {
let object = [{ cod: 'CO',
file: 'CO_SER.csv',
exists: 1},
{ cod: 'ES',
file: 'ES_INS.csv',
exists: 1 } ];
for (let elem of object) {
let insert = `INSERT INTO databaseID VALUES ("${elem.cod}", "${elem.file}", ${elem.exists})`;
}
}
This is what I get:
INSERT INTO databaseID VALUES ("CO", "CO_SER.csv", 1)
INSERT INTO databaseID VALUES ("ES", "ES_INS.csv", 1)
This is what I want to achieve:
INSERT INTO databaseID VALUES ("CO", "CO_SER.csv", 1), ("ES", "ES_INS.csv", 1)

let object = [
{ cod: 'CO',
file: 'CO_SER.csv',
exists: 1},
{ cod: 'ES',
file: 'ES_INS.csv',
exists: 1 } ];
let insert = `INSERT INTO databaseID VALUES ` +
object.map(
elem => `("${elem.cod}", "${elem.file}", ${elem.exists})`
).join(', ')
console.log(insert)

Related

how i replace id with date in json using object.keys, spred operator, and if condition

my json (compact version for better reading)
{
"currency":{
"symbol":"$",
"code":"USD"
},
"prices":{
"data":{
"2022-05-01":{
"property":{
"expectedroomssold_adjusted":6.34,
"exproomssold_l1":3.82
},
"12157":{
"error":false,
"price":7,
"original_price":150.0,
"suggested_price":7,
}
}
}
please tell me the code for replace the date with id in react
You can make a new object and copy all properties of JSON in this object with all properties of date in id and delete the date property from the object
Example
const json = {
a: 5,
b: 7,
"2022-05-22": {
a: 5,
b: 7,
}
};
const id = '2';
const date = "2022-05-22";
const newObj = {
...json,
[id]: {
...json[date]
}
};
delete newObj[date];
console.log(newObj)

Split one object into multiple object by ID

I have an object of the following structure
const obj = {
[name-1]: 'Peter',
[name-2]: 'Mark',
[name-3]: 'Rich',
[age-1]: 25,
[age-2]: 30,
[age-3]: 45
}
I need to split it into 3 separate object like that
const obj1 = {
[name-1]: 'Peter',
[age-1]: 25,
}
const obj2 = {
[name-2]: 'Mark',
[age-2]: 30,
}
const obj3 = {
[name-3]: 'Rich',
[age-3]: 45,
}
How can I achieve that?
Begin by getting entries of the input object using Object.entries
Reduce over the entries generated in step 1
For each iteration's current value split by delimiter '-'
Reduce it to an object, the object will act as a lookup store
Post doing that just extract the values of the lookup.
Transform the individual entries back to its individual object using Object.fromEntries
const obj = {
'name-1': 'Peter',
'name-2': 'Mark',
'name-3': 'Rich',
'age-1': 25,
'age-2': 30,
'age-3': 45
};
const groupedResult = Object.values(Object.entries(obj).reduce((r, c) => {
const [key, val] = c;
const split = key.split('-');
if (!r[split[1]]) {
r[split[1]] = [c];
} else {
r[split[1]].push(c);
}
return r;
}, Object.create(null)))
.map((value) => Object.fromEntries(value));
console.log(groupedResult);

Loop and merge object in Lodash

I have a array of object, like this:
var obj = [{
employeeId: 1300000,
requestValue: 2
}, {
employeeId: 1300000,
requestValue: 3
}, {
employeeId: 1300001,
requestValue: 4
}]
I know how to do it in javascript. But, How can I a retrieve the following result using Lodash:
var result = {
1300000: [
{ requestValue: 2 },
{ requestValue: 3 }
],
1300001: [
{ requestValue: 4 }
]
}
Using Lodash:
Group By with employeeId
mapValues to map each group, and take only requestValue by using
map
Here is the example:
let input = [{"employeeId":1300000,"requestValue":2},{"employeeId":1300000,"requestValue":3},{"employeeId":1300001,"requestValue":4}],
res = _(input)
.groupBy('employeeId')
.mapValues(g => _.map(g, ({requestValue}) => ({requestValue})))
.value();
console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Using Plain JavaScript
Take a hash (resulant) and start with iterating it using reduce
Check if the employee id exists as a key of the hash for each
employeeId, if exist use it otherwise create the key value pair with
a empty array
Take either the existing value (for the key) or the new one and push
requestValue
let input = [{"employeeId":1300000,"requestValue":2},{"employeeId":1300000,"requestValue":3},{"employeeId":1300001,"requestValue":4}],
res = input.reduce((r, {employeeId, requestValue})=> {
(r[employeeId] = r[employeeId] || []).push({requestValue});
return r;
}, {});
console.log(res);

Final Form calculate set object to a position in array values

I am setting up a parser for one of my fields, if user types 'Honda' for example, it will get matching DB values like: {make: 'Honda', id: 5, year: '2012'} and etc.
I would like to set field values when I get response from API.
Is it possible instead of the following
updates(value, name, allValues) => {
return {
['vehicle[0].make']: 'Honda',
['vehicle[0].year']: '2012',
....
}
}
to set values like following:
updates(value, name, allValues) => {
return {
['vehicle[0]']: {
make: 'Honda',
id: 5,
year: '2012'
....
}
}

AWS DyanmoDB Nodejs update array of array

Trying to update array of array in DynamoDB.
This is the schema, I want the data insert into personalData as item of an array
This is the params:
var params = {
TableName: "coupons",
Key: {
'clientId': 'A0002'
},
ReturnValues: 'ALL_NEW',
UpdateExpression: 'set #att = list_append(if_not_exists(#att, :empty_list), :data)',
ExpressionAttributeNames: {
'#att': 'campaigns.personalData'
},
ExpressionAttributeValues: {
':data': [{
couponCode: "S0001",
regDate: "20170728"
}],
':empty_list': []
}
}
But after the code ran, it created a new attribute 'campaigns.personalData', not insert into the original personalData array:

Resources