Parsing and reformatting an object to a simple javascript array [duplicate] - javascript

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 1 year ago.
Suppose I have an object from that has a key and value pair and I want to reformat it into a simple javascript array:
var obj = {STRUCTURE_: '004A006', SEG_ID: '04081591__,0.12,0.25,65850'}
that looks like this:
['004A006', '04081591__,0.12,0.25,65850']
Whats the easiest way to do that?

Use Object.values to get the object's enumerable property values:
var obj = {STRUCTURE_: '004A006', SEG_ID: '04081591__,0.12,0.25,65850'}
const res = Object.values(obj)
console.log(res)

Related

How to get the keyword of a json varible? [duplicate]

This question already has answers here:
How to get all key in JSON object (javascript)
(7 answers)
Closed 2 years ago.
I have a varible, if I print it out, I see this output:
Well, if I'd know the number '118', it would be easy, but in the program where I am using it, I don't know it. So is there any mode to get it without knowing that value?
You could use the Object.keys function to retrieve all keys of your object.
The Object.keys returns an array, you can then access the first element of that array like any other array.
const JSONString = '{"118": {"input1": 6, "input2": 1, "input3": 3}}';
const json = JSON.parse(JSONString);
const keys = Object.keys(json);
console.log(keys[0]);

How to create an object with specific key and value from an array in JavaScript? [duplicate]

This question already has answers here:
How to create an object from an Array of key-value pairs?
(7 answers)
Closed 2 years ago.
I would like to create a JS object made of specific array values. For example, I have an array ["bananas", 5] and I would like to convert it into an object {"bananas" : 5}. Could you please let me know how could I do that?
Object.fromEntries can be used to create an object from an array of arrays:
let input = ["bananas", 5];
let output = Object.fromEntries([input]);
console.log(output);
Where each two-elements array represents a key-value pair from your new object.

Loop through javascript object without knowing names of items [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 3 years ago.
I want to loop through a javascript object passed in like this:
{familyid:434832,groupid:5332,programtypecode:'daycare'}
But I don't know the parameter names. There could be any number of parameters with various different names. I want to get the names of the parameters passed in, and of course also their values. How do I get that?
Use Object.keys()
const o = {
familyid: 434832,
groupid: 5332,
programtypecode: 'daycare'
}
keys = Object.keys(o)
// Do something with your keys, like
for (key of keys) {
console.log(`${key} => ${o[key]}`)
}
If you want the result in form of array of array of arrays then use Object.entries
let obj = {familyid:434832,groupid:5332,programtypecode:'daycare'}
console.log(Object.entries(obj))
If you want to directly loop then use for..in
let obj = {familyid:434832,groupid:5332,programtypecode:'daycare'}
for(let k in obj){
console.log(`${k}:${obj[k]}`)
}

remove property on object if its' key is present on array that contains keys? [duplicate]

This question already has answers here:
One liner to delete multiple object properties
(3 answers)
Closed 3 years ago.
I have an array of keys that can be present as propert on object, if present, I want to remove those properties on it, I know removing a single prop on object like this:
const { 'removedPropOnObj', ...newObj } = obj;
newObj here becomes new object with removed property of removedPropOnObj, i want to do something like that but i have in this case is array of keys like:
['removeKey1','removeKey2','removeKey3']
Help?
You can do that using Object.keys and reduce()
let rmKeys =['removeKey1','removeKey2','removeKey3']
let obj = {
removeKey1:1,
removeKey2:2,
removeKey3:3,
removeKey4:4,
removeKey5:5
}
let newObj = Object.keys(obj).reduce((ac,a) => !rmKeys.includes(a) ?({...ac,[a]:obj[a]}) : ac,{})
console.log(newObj);

Setting JSON key with indexed array value [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I am trying to populate a JSON object by automatically setting the keys for an JSON object based on the string value of another array. For example,
var test = ["a","b"]
{test[0]:"A"}
However, I get a Syntax error when I do this, if I manually set the value as the string as shown in the third line {"a":"A"} this issue does not happen. I've checked that test[0] does indeed print out "a" and its datatype is a string. Is there any reason why this might be happening?
Try the following:
var test = ["a","b"]
var obj = {
[test[0]]:"A"
};
console.log(obj);

Categories

Resources