How to create single array for same key but different values - javascript

How to create single array for same key but different values in nodeJs with unique productId
but having different productImage with same productId i want productImage should be an array
and with same productId, productImages are in this productImage array.
var data = [
{
"productID":18,
"productTitle":"Watch",
"productImage":"1588148225540.jpg"
},
{
"productID":18,
"productTitle":"Watch",
"productImage":"15881482433232.jpg"
},
{
"productID":19,
"productTitle":"Shirt",
"productImage":"1588148214343.jpg"
}
]
My expected output should be:
[
{
"productID":18,
"productTitle":"Watch",
"productImage":[
"1588148225540.jpg",
"15881482433232.jpg"
]
},
{
"productID":19,
"productTitle":"Shirt",
"productImage":[
"1588148214343.jpg"
]
}
]

You can use uniqBy function from lodash library
const result = _.uniqBy(products, 'productID');

Here is an answer
var data = [
{
"productID":18,
"productTitle":"Watch",
"productImage":"1588148225540.jpg"
},
{
"productID":18,
"productTitle":"Watch",
"productImage":"15881482433232.jpg"
},
{
"productID":19,
"productTitle":"Shirt",
"productImage":"1588148214343.jpg"
}
]
let output =[];
data.forEach(function(item) {
var existing = output.filter(function(v, i) {
return v.productID == item.productID;
});
if (existing.length) {
var existingIndex = output.indexOf(existing[0]);
output[existingIndex].productImage =
output[existingIndex].productImage.concat(item.productImage);
} else {
if (typeof item.productImage == 'string')
item.productImage = item.productImage;
item.productThumbImage = [item.productThumbImage];
output.push(item);
}
});

Related

Optimize solution for faster execution - NodeJs

I am trying optimize a solution which is used to group the objects based on value present in another array (mapping ) which is written is NodeJS.
function workerFunction(report) {
const groupedArray = []
const granularityKey = 'Month'
const mapping = ['TestField1', 'TestField2'] //will be dynamic based on json input
let activeIIndex = 0;
while (activeIIndex < report.length) {
const index = groupedArray
.findIndex(item => mapping
.every((column) => item[column] === report[activeIIndex][column]))
if (index === -1) {
report[activeIIndex][report[activeIIndex][granularityKey]] = report[activeIIndex]['Cost']
groupedArray.push(report[activeIIndex])
} else {
groupedArray[index][report[activeIIndex][granularityKey]] = report[activeIIndex]['Cost']
}
activeIIndex++;
}
return groupedArray
}
The input to the workerFunction(report) will look like this (usually the array size will be above 300k),
[
{
"TestField1":"value",
"TestField2":"value2",
"Cost":12.5555,
"Month":10
},
{
"TestField1":"value3",
"TestField2":"value4",
"Cost":142.5555,
"Month":10
},
{
"TestField1":"value6",
"TestField2":"value4",
"Cost":15.87,
"Month":10
},
{
"TestField1":"value3",
"TestField2":"value4",
"Cost":16.5555,
"Month":11
}
]
The expected output after passing this json object through workerFunction will be
[
{
"TestField1":"value",
"TestField2":"value2",
"Cost":12.5555,
"Month":10,
"10":12.5555
},
{
"TestField1":"value3",
"TestField2":"value4",
"Cost":142.5555,
"Month":10,
"10":142.5555,
"11":16.5555
},
{
"TestField1":"value6",
"TestField2":"value4",
"Cost":15.87,
"Month":10,
"10":15.87
},
]

How to append the object into existing json array of objects

I am having json object like below which will be dynamic,
let data_existing= [
{
"client":[
{
"name":"aaaa",
"filter":{
"name":"123456"
}
}
]
},
{
"server":[
{
"name":"qqqqq",
"filter":{
"name":"984567"
}
}
]
},
]
From the inputs i will get an object like below,
let data_new = {
"client":[
{
"name":"bbbbb",
"filter":{
"name":"456789"
}
}
]
}
I need to append this object into the existing "client" json object. Expected output will be like,
[
{
"client":[
{
"name":"aaaa",
"filter":{
"name":"123456"
}
},
{
"name":"bbbb",
"filter":{
"name":"456789"
}
}
]
},
{
"server":[
{
"name":"qqqqq",
"filter":{
"name":"984567"
}
}
]
}
]
And, if the "data_new" is not exists in the main objects, it should as new objects like below, for example,
let data_new = {
"server2":[
{
"name":"kkkkk",
"filter":{
"name":"111111"
}
}
]
}
output will be like,
[
{
"client":[
{
"name":"aaaa",
"filter":{
"name":"123456"
}
},
]
},
{
"server":[
{
"name":"qqqqq",
"filter":{
"name":"984567"
}
}
]
},
{
"server2":[
{
"name":"kkkkk",
"filter":{
"name":"11111"
}
}
]
}
]
I tried the below method, but it is not working as expected. Some help would be appreciated.
Tried like below and not worked as expected,
function addData(oldData, newData) {
let [key, value] = Object.entries(newData)[0]
return oldData.reduce((op, inp) => {
if (inp.hasOwnProperty(key)) {
console.log("111");
op[key] = inp[key].concat(newData[key]);
} else {
console.log(JSON.stringify(inp));
op = Object.assign(op, inp);
}
return op
}, {})
}
Your function seems to work when the key already belongs to data_existing (e.g.: "client").
But you have to handle the second use-case: when the key was not found in the objects of data_existing (e.g.: "server2").
This shall be performed after the reduce loop, adding the new item to data_existing if the key was not found.
Here is an example of how you could achieve that:
function addData(inputData, inputItem) {
const [newKey, newValue] = Object.entries(inputItem)[0];
let wasFound = false; // true iif the key was found in list
const res = inputData.reduce((accumulator, item) => {
const [key, value] = Object.entries(item)[0];
const keyMatch = key === newKey;
if (keyMatch) {
wasFound = true;
}
// concatenate the lists in case of key matching
const newItem = { [key]: keyMatch ? [...value, ...newValue] : value };
return [...accumulator, newItem];
}, []);
if (!wasFound) {
res.push(inputItem); // if key was not found, add item to the list
}
return res;
}
Hope it helps.

change an element in an array of objects

I have data in the form of
data = [
{
"date":"2018-05-18T-6:00:00.000Z",
"something":"something1",
"something":"something1"
},
{
"date":"2018-05-19T-6:00:00.000Z",
"something":"something2",
"something":"something2"
}
]
How do I grab the first element in the objects, edit them, then replace them back in the object?
So it should look like this
data = [
{
"date":"2018-05-18",
"something":"something1",
"something":"something1"
}
{
"date":"2018-05-19",
"something":"something2",
"something":"something2"
}
]
I have tried something like this
var date = [];
const getSessions = () => {
loginService.getUser().then((response) => {
var user_id = response.data.id;
console.log("getUser returning this => ", response.data);
loginService.getUserSessions(user_id).then((response) => {
$scope.sessions = response.data;
for (var i = 0; i < $scope.sessions.length; i++){
date.push($scope.sessions[i].next_class.slice(0,10));
};
$scope.sessions.push(date);
console.log($scope.sessions);
This gets the date shortened but doesn't replace the original date in the object.
You can do something like -
var data = [
{
"date":"2018-05-18T-6:00:00.000Z",
"something":"something1",
},
{
"date":"2018-05-19T-6:00:00.000Z",
"something":"something2"
}
]
data.forEach((record) => {
record.date = record.date.split("T")[0]
})
console.log(data);
You can do this also.
`
newArray = data.map(obj => {
dateIntoString = moment(obj.date).format('YYYY-MM-DD');
obj.date = dateIntoString;
return obj;
});
`

Recursively find keys on an object

I have a javascript object structured like this;
brand: {
group: {
subGroup: {
items: []
},
otherSub: {
items: []
}
}
}
Given an array of keys ['brand', 'group', 'newGroup', 'newSubGroup'] I want to split the keys into found and missing keys. So for the structure above I should get back;
present = ['brand', 'group']
missing = ['newGroup', 'newSubGroup']
I'm using ES6 and have lodash available, but struggling to find a clean way to produce this.
This is not to just check existence, it's recursively find the keys and return those present and the remaining ones.
Here's a pretty sketchy way that works.
const find = (keys, obj) => {
const string = JSON.stringify(obj);
return keys.reduce(({ present, missing }, key) => {
const match = string.match(new RegExp(`"${key}":`));
if (match) {
present.push(key);
} else {
missing.push(key);
}
return { present, missing };
}, { present: [], missing: [] });
}
You can use this function made for you ;)
var getAttrs = function(obj) {
return [].concat.apply([], Object.keys(obj).map(function (key) {
var results = [key]
if (typeof obj[key] === 'object') {
Array.prototype.push.apply(results, getAttrs(obj[key]))
}
return results
}))
}
It return the list of properties and children properties.
getAttrs({brand: {
group: {
subGroup: {
items: []
},
otherSub: {
items: []
}
}
}})
> ["brand", "group", "subGroup", "items", "otherSub", "items"]
And you can use it like so:
var lookingFor = ['brand', 'group', 'newGroup', 'newSubGroup']
var existings = getAttrs(obj)
var missings = []
var presents = []
lookingFor.forEach(attr => {
if (existings.indexOf(attr) === -1) {
missings.push(attr)
} else {
presents.push(attr)
}
})
I wrote a function to recursively get unique keys from a nested object, then filtered the array of all the keys you mentioned checking which were present in the result of my function.
var thisObject = {
brand: {
group: {
subGroup: {
items: []
},
otherSub: {
items: []
}
}
}
};
var arr_full = ['brand', 'group', 'newGroup', 'newSubGroup'] ;
var key_array = [];
function addToKeyArray( key_array, object ){
for( var key in object ){
// only get unique keys
if( key_array.indexOf( key ) === -1 ){
key_array.push( key );
}
// concat the result of calling this function recurrsively on object[key]
key_array.concat( addToKeyArray( key_array, object[key] ) );
}
return key_array;
}
var test = addToKeyArray( [], thisObject );
var missing = arr_full.filter( function( el ) {
return test.indexOf( el ) < 0;
});
console.log( test );
console.log( missing )
You can create recursive function using for...in loop inside another function and return object as result..
var obj = {"brand":{"group":{"subGroup":{"items":[]},"otherSub":{"items":[]}}}}
var keys = ['brand', 'group', 'newGroup', 'newSubGroup'] ;
function findKeys(data, keys) {
keys = keys.slice();
function findPresent(data, keys) {
var result = []
for(var i in data) {
if(typeof data[i] == 'object') result.push(...findPresent(data[i], keys))
var index = keys.indexOf(i);
if(index != -1) result.push(...keys.splice(index, 1))
}
return result
}
return {present: findPresent(data, keys), missing: keys}
}
console.log(findKeys(obj, keys))
To keep things clean and readable you can use "for in", inside a nested function for your recursion.
function recur(obj) {
let preMiss = {
present: [],
missing: []
}
let root = traverse => {
for (let key in traverse) {
if (Array.isArray(traverse[key].items)) {
preMiss.missing.push(key);
}
if (typeof traverse[key] === 'object' && !Array.isArray(traverse[key].items)) {
preMiss.present.push(key);
root(traverse[key])
}
}
}
root(obj);
return preMiss;
}
const object = {
brand: {
group: {
subGroup: {
items: []
},
otherSub: {
items: []
}
}
}
}
console.log(Object.entries(recur(object)));
var toFind = ['brand', 'group', 'newGroup', 'newSubGroup'],
found = [];
var o = {
brand: {
group: {
subGroup: {
items: []
},
otherSub: {
items: []
}
}
}
}
//called with every property and its value
function process(key,value) {
var i = toFind.indexOf(key);
if(i !== -1){
found.push(key);
toFind.splice(i, 1);
}
}
function traverse(o,func) {
if(!toFind.length) return;
for (var i in o) {
func.apply(this,[i,o[i]]);
if (o[i] !== null && typeof(o[i])=="object") {
//going one step down in the object tree!!
traverse(o[i],func);
}
}
}
traverse(o,process);
console.log(found); // present
console.log(toFind); // absent
Traverse method taken from https://stackoverflow.com/a/722732/1335165
Even though this question is a bit older, I want to present a rather short solution to the problem.
const recursivelyGetKeys = obj => Object.keys(obj).map(key => typeof obj[key] === 'object'
? [...recursivelyGetKeys(obj[key]), key] : [key]).reduce((p, c) => [...p, ...c], [])
This function will return all keys in the object, so a call to the array arr with
const arr = {
brand: {
group: {
subGroup: {
items: []
},
otherSub: {
items: []
}
}
}
}
will output:
const keys = recursivelyGetKeys(arr) // = ["items", "subGroup", "items", "otherSub", "group", "brand"]
Now to find the intersection set of this and find = ['brand', 'group', 'newGroup', 'newSubGroup'], do:
const found = keys.filter(key => find.some(val === key))
const missing = keys.filter(key => find.every(val !== key))

How to filter the array json in Ext js 4.1

Consider this as my json data.
I want to filter the "gridValues" in the data using value "LAN". I have used Ext js filter method. It doesn't return the filter data.
var tableData = [
{
"TABLE_TITLE": "Details",
"tableColumns": {
"COLUMNNAME_0": "Interface",
"COLUMNNAME_1": "Conversation",
"COLUMNNAME_2": "Data Flow(KB)"
},
"gridValues": [
{
"COLUMVal_0": "LAN",
"COLUMVal_1": "192.168.9.113 to 61.16.173.233",
"COLUMVal_2": "1132.7"
},
{
"COLUMVal_0": "TATA",
"COLUMVal_1": "192.168.8.67 to 111.221.115.98",
"COLUMVal_2": "619.72"
},
{
"COLUMVal_0": "CITI",
"COLUMVal_1": "192.168.8.60 to 23.6.112.201",
"COLUMVal_2": "619.2"
}
]
}
];
I used the following code for filtering data:
var arry =[];
var fliterarry =[];
var i,u;
for (i=0;i<tableData.length;i++) {
arry.push(tableData[i].gridValues);
}
var arryFtr = arry.filter(function(e){
for (u=0;u<e.length;u++) {
if(e[u].COLUMVal_0 === 'TATA'){
tableData[u].gridValues.push(e[u]);
}
return e[u].COLUMVal_0 == 'TATA';
}
return fliterarry
});
I guess you want to filter data with COLUMVal_0 equals "TATA" , right ?
Try following codes in your function :
var filterArray = [];
for ( tdKey in tableData ) {
for ( gvKey in tableData[tdKey].gridValues ) {
if ( tableData[tdKey].gridValues[gvKey].COLUMVal_0 == "TATA" ) {
filterArray.push(tableData[tdKey].gridValues[gvKey]);
}
}
}
return fliterarry;

Categories