1

I am trying to create a dictionary from some lists using the .push() method.

The result I am trying to achieve is:

{
  "departTo": {
    1: 1.159,
    2: 4.156,
    3: 9.185,
    3: 10.158,
    4: 2.158
  },
  "departTo": {
    1: 2.586,
    2: 5.518,
    3: 11.584,
    3: 7.819,
    4: 3.991
  }
}

Below is a sample of code of what I am trying to do:

console.group("Running Script 1...");

var select_array = [];
var arrive_from, info

var selection_type = ["departTo", "arriveFrom"];
var data_lists_depart = [[1, 1.159], [2, 4.156], [3, 9.185], [4, 10.158], [5, 2.158]];
var data_lists_arrive = [[1, 2.586], [2, 5.518], [3, 11.584], [4, 7.819], [5, 3.991]];

selection_type.forEach(function (selection, i) {
    console.log("selection", i + ": ", selection);
    if (selection === "departTo") data_lists = data_lists_depart;
    else if (selection === "arriveFrom") data_lists = data_lists_arrive;
    data_lists.forEach(function (data_list, ii) {
        console.log("   data_list", ii + ": ", data_list);
        key = data_list[0];
        val = data_list[1];
        select_array.push({selection: {key: val}});
    })
})
console.log("select_array: ", select_array);

console.groupEnd("Running Script 1...");

The result I am getting from the above code is:

[
  {"selection": {"key": 1.159}}, 
  {"selection": {"key": 4.156}}, 
  {"selection": {"key": 9.185}}, 
  {"selection": {"key": 10.158}}, 
  {"selection": {"key": 2.158}}, 
  {"selection": {"key": 2.586}}, 
  {"selection": {"key": 5.518}}, 
  {"selection": {"key": 11.584}}, 
  {"selection": {"key": 7.819}}, 
  {"selection": {"key": 3.991}}
]

Any assistance in getting this into the format I need will be greatly appreciated. Thank you.

1
  • Keys have to be unique. You can't have two 3 keys. Commented Jan 24, 2022 at 4:57

2 Answers 2

1

You can use Object.fromEntries with array#map to generate your result object.

const selection_type = ["departTo", "arriveFrom"],
      data_lists_depart = [[1, 1.159], [2, 4.156], [3, 9.185], [4, 10.158], [5, 2.158]],
      data_lists_arrive = [[1, 2.586], [2, 5.518], [3, 11.584], [4, 7.819], [5, 3.991]],
      values = [data_lists_depart, data_lists_arrive],
      result = Object.fromEntries(selection_type.map((name,i) => ([name, Object.fromEntries(values[i]) ])));
console.log(result);

Sign up to request clarification or add additional context in comments.

2 Comments

Useful code but I need the 'if' and 'else if' statements. I would like to know how to push variable values in as the key instead of the string of the variable name.
Didn't get you. Where do you need conditional statements? What will be expected output?
0

The obvious solution

const departTo = [[1, 1.159], [2, 4.156], [3, 9.185], [4, 10.158], [5, 2.158]];
const arriveFrom = [[1, 2.586], [2, 5.518], [3, 11.584], [4, 7.819], [5, 3.991]];
const result = { 
  departTo: Object.fromEntries(departTo), 
  arriveFrom: Object.fromEntries(arriveFrom) 
};

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.