JavaScript, get one object key name knowing the second [duplicate] - javascript

This question already has answers here:
Iterate through object properties
(31 answers)
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Let's say I have an object that has two key-values:
let object = {name: "abc", id: 12}
But let's say, I don't know one of the key-names, so:
let object = {<unknown_to_me>: "abc", id: 12}
How can I get the first key name if I know the other one? Their positions
THe first one is reachable via:
object.id
Can I get the other one by positions, the ! operator,...?

You could do so:
let object = {name: "abc", id: 12};
let knownKeyName = "id";
let objectKeys = Object.keys(object);
let unknownKeyName = objectKeys[objectKeys.length - 1 - objectKeys.indexOf(knownKeyName)];
console.log(unknownKeyName)

Related

I need to reference the value of the same object on another value [duplicate]

This question already has answers here:
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Closed 1 year ago.
Hello i have an array of objects. Like this:
const obj = [{
count: 10,
value: count*2 // the previous count
}]
How can i reference 'count' on 'value' without having to find the index, or is a way to find the index of 'obj'?
You could take a getter with a reference to the same object.
const
objects = [{
count: 10,
get value () { return this.count * 2; }
}];
console.log(objects[0].value);

Sort array of objects based on the ordered list of values [duplicate]

This question already has answers here:
Sort an array of object by a property (with custom order, not alphabetically)
(7 answers)
Sort array of objects by string property value
(57 answers)
Closed 2 years ago.
const arr = [
{Id:"3",name: "ADMIN"},
{Id:"1",name: "SECURITY"},
{Id:"2",name: "INFORMATION_REPORTING"},
{Id: "23",name: "PAYMENTS_SERVICES"},
{Id: "344",name: "PAYMENT_HUB"},
{Id: "31",name: "RTP"},
{Id: "43",name: "PAYMENTS"},
{Id: "34",name: "GPI_ALERTS"},
{Id: "65",name: "ADMINISTRATION"}
]
I have the arr which has the values as describing here.And I want to reorder the arr using the key name as below, Order to be shown.
ADMIN
ADMINISTRATION
PAYMENTS
RTP
PAYMENTS_SERVICES
INFORMATION_REPORTING
PAYMENT_HUB
SECURITY
GPI_ALERTS
So I want the arr in this order shown above based on name key.
You may use Array.prototype.sort() and compare arr items based on their position (Array.prototype.indexOf()) within orderList
const arr = [{Id:"3",name:"ADMIN"},{Id:"1",name:"SECURITY"},{Id:"2",name:"INFORMATION_REPORTING"},{Id:"23",name:"PAYMENTS_SERVICES"},{Id:"344",name:"PAYMENT_HUB"},{Id:"31",name:"RTP"},{Id:"43",name:"PAYMENTS"},{Id:"34",name:"GPI_ALERTS"},{Id:"65",name:"ADMINISTRATION"}],
orderList = ['ADMIN','ADMINISTRATION','PAYMENTS','RTP','PAYMENTS_SERVICES','INFORMATION_REPORTING','PAYMENT_HUB','SECURITY','GPI_ALERTS'],
result = arr.sort(({name:nameA},{name:nameB}) =>
!orderList.includes(nameA) ?
1 :
!orderList.includes(nameB) ?
-1 :
orderList.indexOf(nameA) - orderList.indexOf(nameB)
)
console.log(result)
.as-console-wrapper{min-height:100%;}

Remove multiple positions in array [duplicate]

This question already has answers here:
Deleting array elements in JavaScript - delete vs splice
(29 answers)
Closed 3 years ago.
I have the following array
myArray = ["zell", "allen", 34, 223344, age , "Stree 45"]
i need to delete every position which be length less than 4
in this case the position 2 and 4
I wrote
for(var i=0; i<myArray.length; i++){
if(myArray[i].trim().length<3){
myArray.splice(i,1);
}
}
but works only with the first one, I need with every one
thanks
You can filter out the items that are shorter then 4 chars:
var myArray = ["zell", "allen", 34, 223344, "age" , "Stree 45"];
var filteredArr = myArray.filter(item => item.toString().length >= 4);
console.log(filteredArr);

Can I assign while desconstructing? [duplicate]

This question already has answers here:
How to get a subset of a javascript object's properties
(36 answers)
One-liner to take some properties from object in ES 6
(12 answers)
Closed 4 years ago.
For example:
const obj = {name: 'jack', age: 18, gender: 'male'}
const {name, age} = obj
const obj2 = {name, age}
I want some key-value of a obj to make up a new obj, above is three line codes , I want to know if there is a way more concise? in one line?

Add an element to a multidimensional object? [duplicate]

This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 6 years ago.
var John = { Cats: 2, Dogs: 3, Turtles: 1 };
var Mary = { Dogs: 0, Parakeets: 3};
How do I append new dimensions after I've already created the objects?
...John now also has 1 Parakeet
...Mary now also has 5 Koi
Just give them a value, like:
John.Parakeet = 1;
You can then later access these properties just like any other properties.
It really isn't hard.

Categories

Resources