How to find keys from undefined schema [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Suppose I have following array
array = [
{
optionA: "QA1",
optionB: "QB1",
optionC: "QC1",
optionD: "QD1",
},
{
optionA: "QA2",
optionB: "QB2",
optionC: "QC2",
optionD: "QD2",
optionE: "QD2",
},
{
optionA: "QA3",
optionB: "QB3",
}
]
Expected output
tableHeader = {OptionA,OptionB,OptionC,OptionD,OptionE}
Wanted display questions data in a table format, which is not organised and has n number for options.

You can get all keys as follows
const array = [{optionA: "QA1",optionB: "QB1",optionC: "QC1",optionD: "QD1",},{optionA: "QA2",optionB: "QB2",optionC: "QC2",optionD: "QD2",optionE: "QD2",},{optionA: "QA3",optionB: "QB3",}]
const allKeys = Object.keys(Object.assign({}, ...array))
console.log(allKeys)

Try this one
let header = new Set();
array.forEach((a) => {
Object.keys(a).forEach((item) => {
header.add(item);
});
});
console.log(header);

Related

Need help creating separate variables from these 2 objects in the arrays [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 2 months ago.
Improve this question
// I wanted to create a variable to hold the month and a variable that hold the number...
var finances = [
['Jan-1999', 867886],
['Feb-1999', 984653],
['Mar-1999', 322012],
['Apr-1999', -69417],
['May-1999', 310503],
['Jun-1999', 522857],
['Jul-1999', 1033096],
['Aug-1999', 604885],
['Sep-1999', -216386],
['Oct-1999', 477532],
['Nov-1999', 893810]];
What you need is map() that returns a new array with the result of a function applied to your initial array. In this case your functions would either select the first element of the subarray (the date), or the second element (the number).
var finances = [
['Jan-1999', 867886],
['Feb-1999', 984653],
['Mar-1999', 322012],
['Apr-1999', -69417],
['May-1999', 310503],
['Jun-1999', 522857],
['Jul-1999', 1033096],
['Aug-1999', 604885],
['Sep-1999', -216386],
['Oct-1999', 477532],
['Nov-1999', 893810]];
const dates = finances.map((e) => e[0]);
const numbers = finances.map((e) => e[1]);
console.log(dates);
console.log(numbers);

Mapping a string into a Object Array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I am fairly new to Javascript is there a way in which we can achieve the following result?
const bikes = ["honda","kawasaki",suzuki]
The main object array can be divided into equal parts based on the number of bikes, like the first few rows with the main object will be "honda", the next few groups of rows "Kawasaki" and if more rows are added to the bikes that divided the main object further into equal parts and update
const mainObject =[
{ name:"john", age:"23"},
{ name:"Max", age:"26"},
{ name:"Tim", age:"28"},
{ name:"Jim", age:"28"},
{ name:"Jacob", age:"28"},
{ name:"Luke", age:"28"}
]
The expected Outcome should be
const mainObject =[
{ name:"john", age:"23",bike:"honda"},
{ name:"Max", age:"26",bike:"honda"},
{ name:"Tim", age:"28",bike:"kawasaki"},
{ name:"Jim", age:"28",bike:"kawasaki"},
{ name:"Jacob", age:"28",bike:"suzuki"},
{ name:"Luke", age:"28", bike:"suzuki"}
]
Something like this should do the trick:
result = mainObject.map((o, i) => ({
...o,
bike: bikes[Math.floor(i * bikes.length / mainObject.length)]
}))

Create JavaScript Object from string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Could you please help me to prepare below Object from string using JS. I am new in JS.
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open new_log jdbc1233q6AiRUSRCczayjz0rw 1 1 603442 0 127.6mb 127.6mb
From above string how do I create below object?
{
"health": "yellow",
"status": "open",
"index": "new_log",
"uuid": "jdbc1233q6AiRUSRCczayjz0rw",
"pri": "1",
"rep": "1",
"docs.count": "603442",
"docs.deleted": "0",
"store.size": "127.6mb",
"pri.store.size": "127.6mb"
}
Here is how you can use split
const str = `health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open new_log jdbc1233q6AiRUSRCczayjz0rw 1 1 603442 0 127.6mb 127.6mb`
const lines = str.split(/\n/); // split lines on \n
const arrs = lines.map(line => line.split(/\s+/)); // split line on whitespace
const obj = {}; // define an object, we could use reduce but this is easier to read
arrs[0].forEach((word,i) => obj[word] = arrs[1][i])
console.log(obj); // show it
console.log(JSON.stringify(obj)); // show the JSON

Javascript sorting array by partial string [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 3 years ago.
Improve this question
I have an array like this
var myArray = [
"J20J205147902_B_B716202",
"R20Q205147902_F_B716202",
"P20W205147_902_alternate1",
"M20K205147_902_alternate4",
"F20G205147_902_alternate3",
"K20J205147_902_alternate2",
"L20H205147_902_main"];
I want to sort this array by this pattern
var map = {
"_F_":1,
"_main":2,
"_alternate1":3,
"_alternate2":4,
"_alternate3":5,
"_alternate4":6,
"_alternate5":7,
"_B_":8
};
So, the output should be like this
"R20Q205147902_F_B716202",
"L20H205147_902_main",
"P20W205147_902_alternate1",
"K20J205147_902_alternate2",
"F20G205147_902_alternate3",
"M20K205147_902_alternate4",
"J20J205147902_B_B716202"
You can use a custom sorting comparator function as follows:
var myArray = [
"J20J205147902_B_B716202",
"R20Q205147902_F_B716202",
"P20W205147_902_alternate1",
"M20K205147_902_alternate4",
"F20G205147_902_alternate3",
"K20J205147_902_alternate2",
"L20H205147_902_main"];
var map = {
"_F_":1,
"_main":2,
"_alternate1":3,
"_alternate2":4,
"_alternate3":5,
"_alternate4":6,
"_alternate5":7,
"_B_":8
}
myArray.sort((...args) => {
const [a, b] = args.map(str => Object.keys(map).find(key => str.includes(key)))
return map[a] - map[b];
})
console.log(myArray)

Access to object property with variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hello so basically I wonder how I can achieve to select property from object with:
objectName.someVariable.
For example how to make this code console.outing number from object:
var a = function(selected){
console.log(selected);
var q = {
black: 1,
red: 2,
blue: 3
}
console.log(q.selected);
console.log(q+'.'+selected);
}
You can access to an object property with:
a point like this: q.black
with two [] like this : q.["black"] or q.[aVariable] //aVariable = "black"
In your example, you can simply do like this:
var a = function(selected){
console.log(selected);
var q = {
black: 1,
red: 2,
blue: 3
}
console.log(q.selected); // Serve to nothing.
console.log(q[selected]+'.'+selected);
}
You can consult w3schools if you want more details.
Tell me if you have some questions.

Categories

Resources