How to delete an empty property of an object? [duplicate] - javascript

This question already has answers here:
How do I remove a key from a JavaScript object? [duplicate]
(3 answers)
Closed last month.
I'm new in JS and I want to delete the property "" of an object like this:
{tecnico2: 1.1, "": 0, tecnico4: 5, tecnico1: 3, tecnico3: 3}
to
{tecnico2: 1.1, tecnico4: 5, tecnico1: 3, tecnico3: 3}
Thank you for your time!!
I search about this but I only saw examples about delete the values '', not the key like I need it.

const data = {tecnico2: 1.1, "": 0, tecnico4: 5, tecnico1: 3, tecnico3: 3}
delete data['']
console.log(data)
or, if you don't want to modify the original data object:
const data = {tecnico2: 1.1, "": 0, tecnico4: 5, tecnico1: 3, tecnico3: 3}
console.log(Object.fromEntries(Object.entries(data).filter(([k])=>k!=='')))

const start = {tecnico2: 1.1, "": 0, tecnico4: 5, tecnico1: 3, tecnico3: 3}
const { ['']: empty, ...arrayWithoutKey } = start
console.log(arrayWithoutKey)
// { tecnico2: 1.1, tecnico4: 5, tecnico1: 3, tecnico3: 3 }

Related

How do I multiply an array of numbers with another array of numbers in javascript

I've been trying to make an array of numbers be able to times another array of numbers without doing array.join("") * array2.join("").
I've tried a lot of methods such as:
var input = [3, 6, 4];
var scalar = 5;
var output = input.map(x => x * scalar); // [15, 30, 20]
Although that's only one number the array can multiply to.
I'd like a function that can do:
var array = [ 1, 3, 2 ];
var array2 = [ 5, 3, 8, 2, 3, 5, 2 ];
someFunction(array, array2);
// [ 7, 1, 0, 4, 7, 0, 4, 6, 4 ]
Please note I don't want it to be something like
array.join("") * array2.join("")
I'm willing to give all my reputation as a bounty if someone is able to answer my question.
If scientific notation is the problem, turn the arrays into BigInts instead.
var array = [ 1, 3, 2, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5 ];
var array2 = [ 5, 3, 8, 2, 3, 5, 2, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5 ];
const someFunction = (arr1, arr2) => [...String(
BigInt(arr1.join('')) * BigInt(arr2.join(''))
)].map(Number);
console.log(someFunction(array, array2));

Map current array with values by index from another array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
Let's imagine I have to arrays:
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const array2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
array2.map((elem, index) => {
// Looking here to return the value from array1 by index
});
From array2 I am looking to return values from array1, by the index position, but just in the range 1 -10. The idea is that it should go in a kind of circle where the start value is 1 and end value is 10.
The expected output for the above example is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
More examples of how it should work:
Index 1 from array2 -> return 1 from array1
Index 10 from array2 -> return 10 from array1
Index 12 from array2 -> return 2 from array1
Index 20 from array2 -> return 10 from array1
Index 999 from array2 -> return 9 from array1
Index 1225 from array2 -> return 5 from array1
You can use the remainder operator (%):
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const array2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
const result = array2.map((elem, index) => array1[index % array1.length]);
console.log(result);
You really don't need to have array2, which provides no information other than its length -- the zeroes are not relevant. So given just the length, it could be as follows:
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const length = 15;
const result = Array.from({length}, (elem, index) => array1[index % array1.length]);
console.log(result);
You probably don't need two arrays to achieve this. You could use the modulo operator to perform 'wrap around' like lookups.
For example:
array1[index % (array1.length)]
if you absolutely need the info in a second array, something like this would achieve the desired result:
array2.forEach((_, index) => array2[index] = array1[index % (array1.length)]);

How to combine values of an object property into one based on a condition- Javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have an object:
obj ={
"AAA": 1,
"BBB": 4,
"CCC": 2,
"DDD": 1,
"EEE": 1,
"FFF": 2,
"AAA_TOTAL": 1,
"BBB_TOTAL": 13,
"GGG": 1,
"HHH": 1,
"III": 14,
"JJJ": 35,
"JJJ_TOTAL": 7,
"KKK": 2,
"LLL": 6,
"MMM_TOTAL": 1,
"OOO": 3,
"PPP": 1
}
now I want to sum up the values of a property that includes TOTAL and has that property name included; Eg:
I want to sum up the values of AAA and similar property AAA_TOTAL => so the output would be :
obj["AAA"] + obj["AAA_TOTAL"]; => o/p would be 2
the TOTAL word can be attached to any property up there. example: it can also be: GGG_TOTAL
in this case, I want to sum up GGG + GGG_TOTAL
Other ex:
obj["BBB_TOTAL"] + obj["BBB"] => o/p would be 17
the final object should looks like:
obj2 ={
"AAA": 2, (sum up value of "AAA_TOTAL": 1)
"BBB": 17,( sum up value of "BBB_TOTAL": 13)
"CCC": 2,
"DDD": 1,
"EEE": 1,
"FFF": 2,
"GGG": 1,
"HHH": 1,
"III": 14,
"JJJ": 42,(sum of "JJJ_TOTAL": 7)
"KKK": 2,
"LLL": 6,
"MMM": 1, (just removing the TOTAL string here and reatining the value as no "MMM" is present to sum)
"OOO": 3,
"PPP": 1
}
this is not a static list of properties and is subject to change, hence I cannot use a hard coded condition.
code:
for (const [key, value] of Object.entries(obj)) {
op={};
id = key.replace(/_total$/gi,'');
op[id] = op[id] || 0;
op[id] += value;
console.log(op)
}
this does not give me desired o/p: it gives this which I'm not looking for:
obj ={
"AAA": 1,
"BBB": 4,
"CCC": 2,
"DDD": 1,
"EEE": 1,
"FFF": 2,
"AAA": 1,
"BBB": 13,
"GGG": 1,
"HHH": 1,
"III": 14,
"JJJ": 35,
"JJJ": 7,
"KKK": 2,
"LLL": 6,
"MMM": 1,
"OOO": 3,
"PPP": 1
}
this just removes the "TOTAL" string from property but does not remove that property itself and does not sum.
any help??
const newObj = Object.keys(obj2).reduce((acc, key) => {
if (key.indexOf('_TOTAL') === -1) {
acc[key] = obj2[key] + (obj2[`${key}_TOTAL`] || 0);
}
return acc;
}, {}),

JavaScript. Push and read from Multidimensional Array

I am trying to push values into a multidimensional array and read values out of it based on code that i've seen on other posts on this site. This is my array push code.
SelectedWindowGraphs.push([ContainerIDValue,elementID+"chkbox"]);
ContainerIDValue is an integer and elementID+"chkbox" is what i want to store at that position in the array. Here is what i saw when i debugged my code:
This is not what i want. At position 0, i want CUT17chkbox, CUT18chkbox, and CUT19chkbox. How do i fix my array so that i does that?
// initialize an array at that position in case it has not been defined yet
SelectedWindowGraphs[ContainerIDValue] = (SelectedWindowGraphs[ContainerIDValue] ||
[]);
// push the value at the desired position
SelectedWindowGraphs[ContainerIDValue].push(elementID+"chkbox");
You have to push to a subarray:
if(!SelectedWindowGraphs[ContainerIDValue])
SelectedWindowGraphs[ContainerIDValue] = [];
SelectedWindowGraphs[ContainerIDValue]
.push(elementID+"chkbox");
You could add elements at certain position just doing:
var arr = [ 1, 2, 3, 4, 5, 6, 7 ]
arr[2] = "three";
console.log(arr);//[ 1, 2, 'three', 4, 5, 6, 7 ]
In a multidimensional array:
var arr = [ 1, [2, 3, 4, 5, 6], 7 ]
arr[1][2] = "four";
console.log(arr);//[ 1, [ 2, 3, 'four', 5, 6 ], 7 ]
When you perform push you are adding one or more elements at the end.
var arr = [1,2,3]
arr.push(4,5);//you are adding 4 and then 5
console.log(arr);//[ 1, 2, 3, 4, 5 ]
In a multidimensional array:
var arr = [1,2,[3,4]]
arr[2].push(5,6);//position 2
console.log(arr);//[ 1, 2, [ 3, 4, 5, 6 ] ]
To insert an element in a specific position (and move right element n positions) you could use splice(). In the following case, 2th and 3th position
var arr = [ 1, 2, 3, 4, 5 ]
arr.splice(2, 0, 999, 8888);
console.log(arr);//[ 1, 999, 8888, 2, 3, 4, 5 ]
In a multidimensional array:
var arr = [ 1, 2, [3,4,5], 6, 7 ]
arr.splice(2, 0, [8,9,10]);
console.log(arr);//[ 1, 2, [ 8, 9, 10 ], [ 3, 4, 5 ], 6, 7 ]

Filter Top X from Array of arrays

Let's say we have an Array which contains arrays inside:
[
["2000-01-01", "xyz1#gmail.com", 1, 9, 338],
["2000-01-01", "xyz2#yahoo.com", 1, 2, 159],
["2000-01-01", "xyz3#yahoo.com", 1, 5, 462],
["2000-01-01", "xyz4#yahoo.com", 1, 6, 417],
["2000-01-01", "xyz5#gmail.com", 1, 3, 156],
["2000-01-01", "xyz6#gmail.com", 1, 8, 414],
]
I want to get top 2 based on the last column i.e.
["2000-01-01", "xyz3#yahoo.com", 1, 8, 462],
["2000-01-01", "xyz4#yahoo.com", 1, 6, 417],
We can use Array.filter but not really sure how to in this situation.
You could sort descending by the element at index 4 and take the first two elements.
This propposal features a destructuring assignment, where an array is taken for destructuring and the property 4 is taken and renamed to a rsp. to b.
Example:
vvv
{ 4: a } = ["2000-01-01", "xyz1#gmail.com", 1, 9, 338]
^
Result
a = 338
var array = [["2000-01-01", "xyz1#gmail.com", 1, 9, 338], ["2000-01-01", "xyz2#yahoo.com", 1, 2, 159], ["2000-01-01", "xyz3#yahoo.com", 1, 5, 462], ["2000-01-01", "xyz4#yahoo.com", 1, 6, 417], ["2000-01-01", "xyz5#gmail.com", 1, 3, 156], ["2000-01-01", "xyz6#gmail.com", 1, 8, 414]],
top2 = array.sort(({ 4: a }, { 4: b }) => b - a).slice(0, 2);
console.log(top2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Without sorting:
function getTopTwo(array){
let first = {4: - Infinity}, second = { 4: - Infinity};
for(const el of array){
if(el[4] > first[4]){
second = first;
first = el;
} else if(el[4] > second[4]){
second = el;
}
}
return [first, second];
}

Categories

Resources