how can i push price into object using javascript? - javascript

Purchasedata.find(function(err, purchasedatas) {
if (err) {
return handleError(res, err);
}
var totalprice = 0;
for (var i = 0; i < purchasedatas.length; i++) {
findProduct(i, function(i, price) {
});
}
function findProduct(i, callback) {
Productpointallocation.find({
'productcode': purchasedatas[i].ItemCode
}).exec(function(err, productpointallocations) {
if (err) {
return handleError(res, err);
}
var pointMultiplier = 0;
if (!productpointallocations) {
pointMultiplier = 0;
} else if (productpointallocations.length == 0) {
pointMultiplier = 0;
}
if (pointMultiplier >= 0) {
var totalprice = (parseFloat(purchasedatas[i].ItemCost.value)) / 10 * pointMultiplier;
purchasedatas.push({price:totalprice,productname:productpointallocations[0].productname});
console.log(purchasedatas);
}
});
}
});
In purchasedata i am getting two objects
[ { _id: 592fbd65304a7315f87d3f40,
ItemCode: '10',
PurchaseQuantity: 3,
ItemCost: 15,
},
{ _id: 592fbd65304a7315f87d3f3f,
ItemCode: '6',
PurchaseQuantity: 1,
ItemCost: 5,
}]
based on ItemCode i am calculating price. after calculating price i want push price and product name into purchasedatas object
purchasedatas.push({price:totalprice,productname:productpointallocations[0].productname});
i wrote above code but i am getting object like this
[ { _id: 592fbd65304a7315f87d3f40,
ItemCode: '10',
PurchaseQuantity: 3,
ItemCost: 15,
},
{ _id: 592fbd65304a7315f87d3f3f,
ItemCode: '6',
PurchaseQuantity: 1,
ItemCost: 5,
},
{ price: 4.5, productname: ' ADAPTER-PCS' } ]
[ { _id: 592fbd65304a7315f87d3f40,
ItemCode: '10',
PurchaseQuantity: 3,
ItemCost: 15,
},
{ _id: 592fbd65304a7315f87d3f3f,
ItemCode: '6',
PurchaseQuantity: 1,
ItemCost: 5,
},
{ price: 4.5, productname: 'ADAPTER-PCS' },
{ price: 1, productname: 'UNIVERSAL AC DC ' } ]
my expectation result after pushing price and productname
[ { _id: 592fbd65304a7315f87d3f40,
ItemCode: '10',
PurchaseQuantity: 3,
ItemCost: 15,
price: 4.5,
productname: 'ADAPTER-PCS'
},
{ _id: 592fbd65304a7315f87d3f3f,
ItemCode: '6',
PurchaseQuantity: 1,
ItemCost: 5,
price: 1,
'productname: 'UNIVERSAL AC DC '
}]

Javascript's Array.push() function will append an object to an array. If you want to add attributes to elements that are in the array already, you'll have to modify the objects. To do that you'll need to find the index of the object in the array, for example by looping over it and comparing the id.
If you want to modify all the Objects inside of the array you can also use Array.map()

You don't say push since the "push" method is only for arrays, but in fact you can add it to the object, for example:
var obj = {a: 1, b:2}
obj["c"] = 3
and it will be like that:
obj = {a: 1, b: 2, c: 3}

It's not about pushing, but about "joining" 2 objects into 1 array element - not tested but "Object.assign" should do the trick.
Purchasedata.find(function(err, purchasedatas) {
if (err) {
return handleError(res, err);
}
var totalprice = 0;
for (var i = 0; i < purchasedatas.length; i++) {
findProduct(i, function(i, price) {
});
}
function findProduct(i, callback) {
Productpointallocation.find({
'productcode': purchasedatas[i].ItemCode
}).exec(function(err, productpointallocations) {
if (err) {
return handleError(res, err);
}
var pointMultiplier = 0;
if (!productpointallocations) {
pointMultiplier = 0;
} else if (productpointallocations.length == 0) {
pointMultiplier = 0;
}
if (pointMultiplier >= 0) {
var totalprice = (parseFloat(purchasedatas[i].ItemCost.value)) / 10 * pointMultiplier;
purchasedatas[i] = Object.assign({},purchasedatas[i], {price:totalprice,productname:productpointallocations[0].productname});
console.log(purchasedatas);
}
});
}
});
EDIT: it seems that there's some mongo object instead of simple key-value pairs. Try something like that:
if (pointMultiplier >= 0) {
var totalprice = (parseFloat(purchasedatas[i].ItemCost.value)) / 10 * pointMultiplier;
purchasedatas[i] = Object.assign({},JSON.parse(JSON.stringify(purchasedatas[i])), {price:totalprice,productname:productpointallocations[0].productname});
console.log(purchasedatas);
}
or
if (pointMultiplier >= 0) {
var totalprice = (parseFloat(purchasedatas[i].ItemCost.value)) / 10 * pointMultiplier;
purchasedatas[i] = Object.assign({},tojson(purchasedatas[i]), {price:totalprice,productname:productpointallocations[0].productname});
console.log(purchasedatas);
}

So here is it how I believe it would happen,
First iterate over the array (purchasedatas) then over each object inside it:
for (var i = 0; i < purchasedatas.length; i++) {
for (var key in obj) {
if(purchasedatas[i]["ItemCode"] === req.body.ItemCode) {
purchasedatas[i]["price"] = req.body.price;
purchasedatas[i]["productname"] = req.body.productname;
}
}
}
return purchasedatas;
Hope that this is what you want or at least what you are asking for.

Related

remove the second occurrence of array object with same name

I have this array (java script array of objects)
users=[{name:'arrow',age:50,id:444}
{name:'bow',age:66,id:884}
{name:'arrow',age:30,id:99},
{name:'apple',age:50,id:999}
{name:'bow',age:50,id:9669}]
I want to remove second occurrence of same name , in this case , I want to remove {name:'arrow',age:30,id:99} and {name:'bow',age:50,id:9669} and retain first occurrences{name:'arrow',age:50,id:444} and {name:'bow',age:66,id:884}
Resulting array should be :
users= [{name:'arrow',age:50,id:444}
{name:'bow',age:66,id:884},
{name:'apple',age:50,id:999}]
const users = [
{ name: 'arrow', age: 50, id: 444 },
{ name: 'bow', age: 66, id: 884 },
{ name: 'arrow', age: 30, id: 99 },
{ name: 'apple', age: 50, id: 999 },
{ name: 'bow', age: 50, id: 9669 }
]
const uniqueUsers = users.reduce((acc, user) => {
if (!acc.find(u => u.name === user.name)) {
acc.push(user)
}
return acc
}, [])
I'd go with the approach of array.filter:
function removeDuplicateKeyFromArray(arrayOfObjects,keyName){
keyHolder = {}
arrayOfObjects.filter((obj)=>{
if(keyHolder[obj.keyName]){
return false
}
keyHolder[obj.keyName] = 1 //or true
return true
})
}
I would create 2 for-loops, to filter out any duplicates.
here's my code:
let users = [{name:'arrow',age:50,id:444},
{name:'bow',age:66,id:884},
{name:'arrow',age:30,id:99},
{name:'apple',age: 50,id: 990},
{name:'bow',age: 50,id: 9669}]
for (let i = 0; i < users.length; i++) {
for(let x = 0; i < users.length; i++) {
if(users[i].name == users[x].name) {
users.splice(users[x], 1)
}
}
}

Sum values depending on other value in array

I have an array of objects that represent transactions of shares:
[{
date : ...,
symbol: 'TSLA',
amount: 3,
price: 1000.00
},
{
date : ...,
symbol: 'AAPL',
amount: 1,
price: 1200.00
},
{
date : ...,
symbol: 'AAPL',
amount: 7,
price: 1300.00
}]
I need to get sum of amounts based of symbol of that array, so output would be:
[{
symbol: 'TSLA',
amount: 3,
},
{
symbol: 'AAPL',
amount: 8,
}]
Is there an efficient way to do this with build in operations in javascript, or is the only way to do it with 2 array and double loop?
I was thinking of saving symbols in separate Set, and then suming all amounts, but is there a better way?
I've tried this, but this seems to only copy the original array.
const checkIfExists = (array, value) => {
array.forEach((el, i) => {
if (el.symbol === value) {
return i;
}
});
return -1;
};
const calculateSameValues = (data) => {
let result = [];
data.forEach((el) => {
const index = checkIfExists(result, el.symbol);
if (index === -1) {
result.push({symbol: el.symbol, amount: el.amount});
} else result[index].amount += el.amount;
});
console.log(result);
};
Seems like my checkIfExists function was returning always -1.
I fixed it by saving index in seperate variable and than returning it.
Here's code:
const checkIfExists = (array, value) => {
let index = -1;
array.forEach((el, i) => {
if (el.symbol === value) {
console.log(i);
index = i;
}
});
return index;
};
Note that this still uses 2 loops, I was looking for something more efficient, but this works.
you can use array.reduce() something like this:
const arr = [{
symbol: 'TSLA',
amount: 3,
price: 1000.00
},
{
symbol: 'AAPL',
amount: 1,
price: 1200.00
},
{
symbol: 'AAPL',
amount: 7,
price: 1300.00
}]
const x = arr.reduce(function(acc, cur) {
const idx = acc.findIndex(el => el.symbol === cur.symbol);
const obj = {
symbol: cur.symbol,
amount: cur.amount,
}
if(idx < 0) {
acc.push(obj)
} else {
acc[idx].amount = acc[idx].amount + cur.amount;
}
return acc;
}, []);
console.log(x);

Javascript loop through array of strings and ints

Having this array:
const xx = [
{
name: "Alex",
income: 324300,
total: 3030000
},
{
name: "Snake",
income: 3433000,
total: 34323000
},
{
name: "Wake",
income: 4,
total: 3
}
];
I want to loop through it and do something if the value is an int (income and total). How do i go about doing it?
This is what i have tried so far, i dont really know how to take out the objects value and test if it is an int
for (var key in xx) {
var arr = xx[key];
for( var i = 0; i < arr.length; i++ ) {
var obj = arr[ i ];
}
}
}
try:
const xx = [
{
name: "Alex",
income: 324300,
total: 3030000
},
{
name: "Snake",
income: 3433000,
total: 34323000
},
{
name: "Wake",
income: 4,
total: 3
}
];
xx.forEach(item => {
Object.keys(item).forEach(key => {
if (!isNaN(parseInt(item[key])) && isFinite(item[key])) {
// do something here
console.log('INT: ' + item[key]);
}
});
});
You need to exchange internal for with the external. and use:
typeof xx[i][k] === "number" OR xx[i][k] === +xx[i][k] to check if value is
a number.
parseInt(xx[i][k]) === xx[i][k] to check if value is an integer
number.
See example:
const xx = [{
name: "Alex",
income: 324300,
total: 3030000
}, {
name: "Snake",
income: 34.33000,
total: 34323000
}, {
name: "Wake",
income: 4,
total: 3
}];
for (var i = 0; i < xx.length; i++) {
for (var k in xx[i]) {
//if(parseInt(xx[i][k]) === xx[i][k]) //integer
//if(typeof xx[i][k] == "number") //number
if (xx[i][k] === +xx[i][k]) { //number
console.log(k + ": " + xx[i][k]);
}
}
}

combine array of objects by key

I am trying to combine/merge 2 array of objects by key in my case id.
Objective:
I am expecting a results where I would have array containing all objects with ids 1,2,3,4 as per example
Order of merging should not affect number of objects in result for example combine(arr1,arr2) or combine(arr2,arr1) should have array with same number of objects
Order of merging can only affect resulting object for example in case of combine(arr1,arr2) arr2 key,values pair can override arr1 key,values just like deep jquery extend $.extend( true, arr1ObJ,arr2ObJ );
JSFIDDLE: https://jsfiddle.net/bababalcksheep/u2c05nyj/
Sample Data:
var arr1 = [{
id: 1,
name: "fred",
title: "boss"
}, {
id: 2,
name: "jim",
title: "nobody"
}, {
id: 3,
name: "bob",
title: "dancer"
}];
var arr2 = [{
id: 1,
wage: "300",
rate: "day"
}, {
id: 2,
wage: "10",
rate: "hour"
}, {
id: 4,
wage: "500",
rate: "week"
}];
var Result = [{
"id": 1,
"name": "fred",
"title": "boss",
"wage": "300",
"rate": "day"
}, {
"id": 2,
"name": "jim",
"title": "nobody",
"wage": "10",
"rate": "hour"
}, {
id: 3,
name: "bob",
title: "dancer"
}, {
id: 4,
wage: "500",
rate: "week"
}];
Here's a solution. It basically goes through each element of arr2 and checks to see if there's an element with a matching ID arr1. If so, it updates the matching element in arr1 with arr2's values. If there is no match, it simply pushes the element in arr2 onto arr1.
var arr1 = [{id: 1,name: 'fred',title: 'boss'},
{id: 2,name: 'jim',title: 'nobody'},
{id: 3,name: 'bob',title: 'dancer'}];
var arr2 = [{id: 1,wage: '300',rate: 'day'},
{id: 2,wage: '10',rate:'hour'},
{id: 4,wage: '500',rate: 'week'}];
function combineArrays(arr1, arr2) {
for(var i = 0; i < arr2.length; i++) {
// check if current object exists in arr1
var idIndex = hasID(arr2[i]['id'], arr1);
if(idIndex >= 0){
//update
for(var key in arr2[i]){
arr1[idIndex][key] = arr2[i][key];
}
} else {
//insert
arr1.push(arr2[i]);
}
}
return arr1;
}
//Returns position in array that ID exists
function hasID(id, arr) {
for(var i = 0; i < arr.length; i ++) {
if(arr[i]['id'] === id)
{
return i;
}
}
return -1;
}
var combine = combineArrays(arr1, arr2);
output(combine);
/* pretty Print */
function output(inp) {
var str = JSON.stringify(inp, undefined, 4);
$('body').append($('<pre/>').html(str));
}
var arr1 = [{
id: 1,
name: 'fred',
title: 'boss'
}, {
id: 2,
name: 'jim',
title: 'nobody'
}, {
id: 3,
name: 'bob',
title: 'dancer'
}];
var arr2 = [{
id: 1,
wage: '300',
rate: 'day'
}, {
id: 2,
wage: '10',
rate: 'hour'
}, {
id: 4,
wage: '500',
rate: 'week'
}];
function combineArrays(arr1, arr2) {
for (var i = 0; i < arr2.length; i++) {
var idIndex = hasID(arr2[i]['id'], arr1);
if (idIndex >= 0) {
for (var key in arr2[i]) {
arr1[idIndex][key] = arr2[i][key];
}
} else {
arr1.push(arr2[i]);
}
}
return arr1;
}
function hasID(id, arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i]['id'] === id) {
return i;
}
}
return -1;
}
var combine = combineArrays(arr1, arr2);
output(combine);
/* pretty Print */
function output(inp) {
var str = JSON.stringify(inp, undefined, 4);
$('body').append($('<pre/>').html(str));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
How about something along the lines of this:
function combineArrays(arr1, arr2, keyFunc) {
var combined = [],
keys1 = arr1.map(keyFunc),
keys2 = arr2.map(keyFunc),
pos1 = keys1.map(function (id) {
return keys2.indexOf(id);
}),
pos2 = keys2.map(function (id) {
return keys1.indexOf(id);
});
arr1.forEach(function (item, i) {
combined.push( $.extend(item, arr2[pos1[i]]) );
});
arr2.forEach(function (item, i) {
if (pos2[i] === -1) combined.push( item );
});
return combined;
}
used as
var combine = combineArrays(arr1, arr2, function (item) {
return item.id;
});
var arr1 = [
{ id: 1, name: 'fred', title: 'boss' },
{ id: 2, name: 'jim', title: 'nobody' },
{ id: 3, name: 'bob', title: 'dancer' }
];
var arr2 = [
{ id: 1, wage: '300', rate: 'day' },
{ id: 2, wage: '10', rate: 'hour' },
{ id: 4, wage: '500', rate: 'week' }
];
function combineArrays(arr1, arr2, keyFunc) {
var combined = [],
keys1 = arr1.map(keyFunc),
keys2 = arr2.map(keyFunc),
pos1 = keys1.map(function (id) {
return keys2.indexOf(id);
}),
pos2 = keys2.map(function (id) {
return keys1.indexOf(id);
});
arr1.forEach(function (item, i) {
combined.push( $.extend(item, arr2[pos1[i]]) );
});
arr2.forEach(function (item, i) {
if (pos2[i] === -1) combined.push( item );
});
return combined;
}
var combine = combineArrays(arr1, arr2, function (item) {
return item.id;
});
output(combine);
//
//
//
/* pretty Print */
function output(inp) {
var str = JSON.stringify(inp, undefined, 4);
$('body').append($('<pre/>').html(str));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Object returning as undefined

If I call the find function APP.count() all is ok I am getting the correct result but when I call APP.add() I getting this.basket is undefined. I don't get why this is happening?
var APP = (function() {
var
basket = [
{ id: 100, price: 10, description: '', name: 'item one', quantity: 10, url: '' },
{ id: 200, price: 20, description: '', name: 'item two', quantity: 15, url: '' }
],
find = function(item) {
for(var i = 0; i < this.basket.length; i++) {
if(this.basket[i].id === item) {
return i
}
}
return null
},
add = function(item) {
var itemFound = find(item)
},
count = function() {
var total = 0;
for(var i = 0; i < this.basket.length; i++) {
total = total + this.basket[i].quantity
}
return total
};
return {
basket: basket,
find: find,
add: add,
count: count
};
})();
APP.count() /* works */
APP.add() /* returns this.basket as undefined */
The problem is from the call of find(item) in the add function.
Calling find function like that will not use the context of APP object as this, so this.basket will be undefined.
You can check what is the current context this with a simple console.log(this)
So if you want to call the find function with the context of APP, in the add function you need to call this.find(item)
Hi the issue is with the this reference when you call find method by add()
add = function(item) {
var itemFound = find(item)
},
this keyword is referring the window object
try with this below is the running code
var APP = (function() {
var
basket = [
{ id: 100, price: 10, description: '', name: 'item one', quantity: 10, url: '' },
{ id: 200, price: 20, description: '', name: 'item two', quantity: 15, url: '' }
],
find = function(item) {
for(var i = 0; i < this.basket.length; i++) {
if(this.basket[i].id === item) {
return i
}
}
return null
},
add = function(item) {
var itemFound = this.find(item) //changed
return itemFound; //changed
},
count = function() {
var total = 0;
for(var i = 0; i < this.basket.length; i++) {
total = total + this.basket[i].quantity
}
return total
};
return {
basket: basket,
find: find,
add: add,
count: count
};
})();

Categories

Resources