There is the following example:

function _createProductModel(fields) { var productModel = {}; for (var field of fields) { var fieldArr = field.split('-'); if (fieldArr[1] === 'id') { switch (fieldArr[0]) { case 'product': productModel.product_id = Number; break; case 'variant': productModel.variant_id = Number; break; } } else if (fieldArr[1] === 'other') { switch (fieldArr[0]) { case 'product': if (!productModel.product) productModel.product = {}; productModel.product[fieldArr[2]] = String; break; case 'variant': if (!productModel.variant) productModel.variant = {}; productModel.variant[fieldArr[2]] = String; break; } } else if (fieldArr[1] === 'property') { if (!productModel.properties) productModel.properties = {}; productModel.properties[fieldArr[2]] = String; } else if (fieldArr[1] === 'option') { if (!productModel.options) productModel.options = {}; productModel.options[fieldArr[2]] = String; } else if (fieldArr[1] === 'field') { if (!productModel.fields) productModel.fields = {}; productModel.fields[fieldArr[2]] = String; } } return productModel; } // Принимаем массив fields следующего вида var arr = [ "product-id", "variant-other-sku", "product-field-51092", "product-property-19403737", "variant-option-1265700", "variant-other-barcode" ] const Product = _createProductModel(arr); console.log(Product) /* // выходит это { product_id: [Function: Number], variant: { sku: [Function: String], barcode: [Function: String] }, fields: { '51092': [Function: String] }, properties: { '19403737': [Function: String] }, options: { '1265700': [Function: String] } } //нужно примерно такое: { product_id: [Function: Number], variant: { sku: [Function: String], barcode: [Function: String] }, fields: { '51092': [Function: String] }, properties: { '19403737': [Function: String] }, options: { '1265700': [Function: String] }, // Сюда сохраняются ссылки на свойства обьекта, индексы которых соответсвуют индексам входящего массива fields indexes: [product_id, variant.sku, fields.51092...] } */ // вот так планируется использовать var file = [ ['test1', 'test2', 'test3', 'test4', 'test5', 'test6'], ['test1', 'test2', 'test3', 'test4', 'test5', 'test6'], ['test1', 'test2', 'test3', 'test4', 'test5', 'test6'], ['test1', 'test2', 'test3', 'test4', 'test5', 'test6'] ] fileArr = []; for (var i = 0, len0 = file.length; i < len0; i++) { var product = new Product; for (var x = 0, len1 = file[i].length; x < len1; i++){ product.indexes[x] = file[i][x] } fileArr.push(product) } 

The principle of operation in my understanding should be the following: a model is formed, where the data is then written when parsing the csv file, the indexes object should have references to its own properties, whose indices correspond to the indices of the incoming fields array, this is necessary so that every time where to thrust (as in csv there are tens, and even hundreds of thousands of lines). Help please solve this problem.

  • Problem solved by eval ('') method - Nikolay Muravets

0