D
D
Dark_Scorpion2014-11-04 15:13:12
JavaScript
Dark_Scorpion, 2014-11-04 15:13:12

How to process dynamic tables with correct data unloading?

It is necessary to process the data from the table correctly by moving the data to the object for subsequent unloading. Headings and their order in the table is different.
Example: for one table: car brand, engine brand, engine size, release date. The second one: brand of car, article number, engine type, engine size. And there are many options, but it is necessary that the object contains all possible fields and correctly assigns values ​​depending on the headers. How to implement this in javascript, or an example of an algorithm in another language.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dark_Scorpion, 2015-07-11
@Dark_Scorpion

I saw notifications about my question, and decided to unsubscribe how I solved the problem on js then
1) I created a template object that has all the necessary fields and cloned it when I created a new one.
2) Created an associative array of matches, for example {car: car, release date: releaseDate} (titleArr)
3) Iterated through the first row of the table, reading the value, looking for it in the array of matches, and writing the English name from the array. titleArr -> keyArr
4) And then just ran through the row of the table and wrote down the first value with the first key from the final array p.3, the second with the second, etc. resultObj[keyArr[i]] = rowDataArr[i];
IMPORTANT: This algorithm has a weak point that must be taken into account: you need to know ALL possible column names for step 2. Otherwise, you may lose data and, moreover, the entire table will move out. Just in case, it is worth checking the lengths of arrays from point 3 (tirleArr.length == keyArr.length) If suddenly the desired value in the array does not match, then the lengths will be different and you will know where to look for the problem.

A
Alexander Kaloshin, 2014-11-05
@undassa

try to make an object containing all possible table fields:

var etalon = {
   type: '',
   engine: '',
   date : ""
}

then take the incomplete object you have, e.g.
var car = {
   type: 'tesla',
   date : '01.11.2014'
}

and make a new object with inheritance (for example, through the lodash library):
var a = _.extend({},etalon,car);

console.log(a);
/*
{
   type: 'tesla',
   engine: '',
   date : "01.11.2014"
}
*/

get the object you need, where the fields that are not filled in will be filled with default values.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question