M
M
Millerish2016-10-13 18:49:52
JavaScript
Millerish, 2016-10-13 18:49:52

js. How to transfer data from one array to another?

Good time of the day!

Please help me with my array problem. It is necessary to shift data from a multidimensional array to a one-dimensional one. If the name matches (Euroshop, Tur), do not add the element and add the sum(2500+2500) and the area(7500) .

Initial data:

var m = [ ['Euroshop', 2500, 7500] , ['Euroshop', 2500, 7500], ['Tur', 2500, 7500] ];
var TOTAL_PAINTING = 3;
var m2 = [];


The output should look like this:
m2 = ["Euroshop", 5000, 15000, "Tur", 2500 7500];

I try like this:
for (var i = 0; i < TOTAL_PAINTING; i++) {
            if (m2.length == 0){
                m2.push(m[i][0]);
                m2.push(m[i][1]);
                m2.push(m[i][2]);
            }
            else {
                debugger;
                var status = true;
                var price = 0;
                for (var j = 0; j < m2.length; j++) {
                    if (m2[j] == m[i][0]) {
                        for (var g = m2.length; g >= 0; g--) {
                            if (m2[j] == m[i][0]){
                                m2[j+1] = m2[j+1] + m[i][1]
                            }
                        }
                        status = false;
                    }
                }
                if (status == true){
                    m2.push(m[i][0]);
                    m2.push(price);
                    m2.push(m[i][2]);
                }
            }

        }

Where is the mistake??? How right?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Millerish, 2016-10-13
@Millerish

for (var i = 0; i < TOTAL_PAINTING; i++) {
            if (m2.length == 0){
                m2.push(m[i][0]);
                m2.push(m[i][1]);
                m2.push(m[i][2]);
            }
            else {
                var status = true;
                var price = 0;
                for (var j = 0; j < m2.length; j++) {
                    if (m2[j] == m[i][0]) {
                        for (var g = m2.length; g >= 0; g--) {
                            if (m2[g] == m[i][0]){
                                m2[j+1] = m2[j+1] + m[i][1]
                                m2[j+2] = m2[j+2] + m[i][2]
                            }
                        }
                        status = false;
                    }
                }
                if (status == true){
                    m2.push(m[i][0]);
                    m2.push(m[i][1]);
                    m2.push(m[i][2]);
                }
            }

        }

D
Deodatuss, 2016-10-13
@Deodatuss

Of course, it doesn’t look good, but it’s not necessary that the repetitions are one after another:

var m = [
  ['Euroshop', 2500, 7500],
  ['Euroshop', 2500, 7500],
  ['Tur', 2500, 7500]
];
var flatArr = m.join(',').split(',');
var resultObj = {};
for (var i = 0; i < flatArr.length; i += 3) {
  var name = flatArr[i];
  var firstVal = +flatArr[i + 1];
  var secondVal = +flatArr[i + 2]
  resultObj[name] = resultObj[name] || {};
  resultObj[name]["first"] = resultObj[name]["first"] ? resultObj[name]["first"] + firstVal : firstVal;
  resultObj[name]["second"] = resultObj[name]["second"] ? resultObj[name]["second"] + secondVal : secondVal;
}
var resultArr = [];
for (var key in resultObj) {
  resultArr.push(key);
  resultArr.push(resultObj[key]["first"]);
  resultArr.push(resultObj[key]["second"]);
}
alert(resultArr);

R
Rsa97, 2016-10-13
@Rsa97

var m = ;
var temp = m.reduce(function(prev, el) {
   if (prev.hasOwnProperty([el[0]]))
     prev[el[0]] = [prev[el[0]][0]+el[1], prev[el[0]][1]+el[2]];
   else
     prev[el[0]] = [el[1], el[2]];
   return prev;
  }, {});
console.log(temp);
var result = [];
var i = 0;
for (var name in temp)
  result[i++] = [name, temp[name][0], temp[name][1]];
console.log(result);

K
Konstantin Gromov, 2016-10-14
@Pathgk

If we decompose the task into two stages: addition and filling the final array, then there is much less mess and, accordingly, the probability of an error.

var TOTAL_PAINTING = 3;

var m1 = ,
    m2 = [],
    helper = {};

for (var i = 0; i < TOTAL_PAINTING; i++) {
    var name = m1[i][0],
        data = m1[i].slice(1);

    if (name in helper) {
        helper[name][0] += data[0];
        helper[name][1] += data[1];
    } else {
        helper[name] = data;
    }
}

for (var key in helper) {
    m2.push(key, helper[key][0], helper[key][1]);
}

console.log(m2);

I
Igor Shevchenko, 2016-11-15
@igor_shevchenko

The error occurs because ConfigParser- this is a module of the standard library, which in the third python was renamed to configparser. Therefore, he is not with you. This module is required because you are installing a second python package. Trysudo apt-get install python3-psycopg2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question