JavaScript Javascript write new object based on the keys and values of another object javascript write object to file,javascript write object to console,

You could iterate only the elements from index 1 and greater and take the object at index zero as blue print for the objects.

var data = [{ A: "Name", B: "Type", C: "Company", D: "Year", E: "Alt1", F: "Name1", G: "Alt2", H: "Name2", I: "Notes" }, { A: "Skittles", C: "Mars", D: 0, E: "Cadbury", F: "Sour Patch Kids", I: "CLEAN" }, { A: "Love", B: "Chocolate", C: "Dove", D: 0, E: "0", F: "0", }],
    keys = Object.keys(data[0]),
    result = data.slice(1).map(function (o) {
        var temp = {};
        keys.forEach(function (k) {
            temp[data[0][k]] = k in o ? o[k] : '';
        });
        return temp;
    });

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

You can simplify the process by using Array methods like forEach and map in conjunction with Object.keys.

var array = [
    {
        "A": "Name",
        "B": "Type",
        "C": "Company",
        "D": "Year",
        "E": "Alt1",
        "F": "Name1",
        "G": "Alt2",
        "H": "Name2",
        "I": "Notes"
    },
    {
        "A": "Skittles",
        "C": "Mars",
        "D": 0,
        "E": "Cadbury",
        "F": "Sour Patch Kids",
        "I": "CLEAN"
    },
    {
        "A": "Love",
        "B": "Chocolate",
        "C": "Dove",
        "D": 0,
        "E": "0",
        "F": "0",
    }
]

var master = array.shift()

master = Object.keys(master).map(function (k) { return [k, this[k]] }, master)

array.forEach(function (e) {
  master.forEach(function (pair) {
    var value = pair[0] in e ? e[pair[0]] : defaultValueFor(pair[0])
    delete e[pair[0]]
    e[pair[1]] = value
  })
})

console.log(array)


function defaultValueFor (key) {
  return key === 'Year' ? 0 : ''
}
Answer:2

Here's a step-by-step breakdown of one way of getting this done. This does have the advantage of not needing any ternary or if blocks...

var user_file = [
    {
        "A": "Name",
        "B": "Type",
        "C": "Company",
        "D": "Year",
        "E": "Alt1",
        "F": "Name1",
        "G": "Alt2",
        "H": "Name2",
        "I": "Notes"
    },
    {
        "A": "Skittles",
        "C": "Mars",
        "D": 0,
        "E": "Cadbury",
        "F": "Sour Patch Kids",
        "I": "CLEAN"
    },
    {
        "A": "Love",
        "B": "Chocolate",
        "C": "Dove",
        "D": 0,
        "E": "0",
        "F": "0",
    }
];
// split off the first set of keys
var keys = user_file[0];
// keep the data separate
var data = user_file.splice(1);
// iterate over the data
var result = data.map(function (item) {
   // item is now one of the last two items
   // get the keys for that item
   var itemKeys = Object.keys(item); 
   // Make a blank
   var newItem = {};
   // iterate over the keys
   itemKeys.forEach(function (key) {
      // get the "real" key name
      var realKey = keys[key]; 
      // set the value of that key to the value of the item
      newItem[realKey] = item[key];
   });
   // add to result
   return newItem;
});
console.log(result);
Answer:3

I am currently struggling copying the innerHTML part of classes to another div. The problem is that I am using the same class name for every div but each div has another content. I want to achieve ...

I am currently struggling copying the innerHTML part of classes to another div. The problem is that I am using the same class name for every div but each div has another content. I want to achieve ...

I need some help with RegEx, it may be a basic stuff but I cannot find a correct way how to do it. Please help! So, here's my question: I have a list of URLs, that are invalid because of double ...

I need some help with RegEx, it may be a basic stuff but I cannot find a correct way how to do it. Please help! So, here's my question: I have a list of URLs, that are invalid because of double ...

When I try to create a token for a user like in this code : const jwt = require('jsonwebtoken'); const passport = require('passport'); const Patient = require('../models').Patient; module.exports = {...

When I try to create a token for a user like in this code : const jwt = require('jsonwebtoken'); const passport = require('passport'); const Patient = require('../models').Patient; module.exports = {...

Currently in React, I am using array.map(function(text,index){}) to iterate through an array. But, how am I going to iterate through two arrays simultaneously using map? EDIT var sentenceList = ...

Currently in React, I am using array.map(function(text,index){}) to iterate through an array. But, how am I going to iterate through two arrays simultaneously using map? EDIT var sentenceList = ...