If this is your first visit, check our documentation. To post, register or login.
Forums / The Zen Pub / Javascript help , maybe

Javascript help , maybe

Started by mesnitu on 15 Aug 2018, 3:56 PM · Views: 52

Results 1 to 2 of 2
15 Aug 2018, 3:56 PM
#1
mesnitu avatar

mesnitu

Totally Zenned

Join Date:
Aug 2014
Location:
Lisbon
Posts:
597
Plugin Contributions:
0

Javascript help , maybe

Hi
This is not related to zencart code, actually it's for a google sheets, so it's a pub question (?), maybe someone can help, maybe not.
The goal it's to create a array that aggregates the "warehouse" name with a array of fields to be sent as email. So instead of sending emails line by line, I'm trying to send 1 email to the respective warehouse, with all the related fields.
In PHP I would do this:

$orders = array();
// Loop is getting the sheet rows values
$loop = array('0' => array('warehouse', 'title1', '1111'),
			  '1' => array('warehouse', 'title2', '2222'),
			  '2' => array('warehouse1', 'title3', '3333'),
			  '3' => array('warehouse2', 'title4', '4444'),
			  '4' => array('warehouse1', 'title4', '4444')
			 );

for ($i = 0; $i < count($loop); ++$i) {
	$row = $loop[$i];
	$orders[$row[0]][] = $row[1] . ' - ' . $row[2];

}
/** This is what I want in javascript
Array
(
    [warehouse] => Array
        (
            [0] => title1 - 1111
            [1] => title2 - 2222
        )

    [warehouse1] => Array
        (
            [0] => title3 - 3333
            [1] => title4 - 4444
        )

    [warehouse2] => Array
        (
            [0] => title4 - 4444
        )

)
*/

foreach ($orders as $sendTo => $fields) {
	print_r($sendTo); // send email to 
	foreach($fields as $field) {
		print_r($field); // with this fields
	}
}

In Javascript it´s hell :frusty: :

var loop = [
["warehouse","title1","1111"],
["warehouse","title2","2222"],
["warehouse1","title3","3333"],
["warehouse2","title4","4444"],
["warehouse1","title4","4444"]
];

var orders = {};

for (var i = 0; i < loop.length; ++i) {

var row = loop[i];

orders[row[0]] = [];
orders[row[0]].push([ row[2] + ' - ' + row[1] ]);

}
console.log(orders);

I've tried so much... but all wrong.

“Though the problems of the world are increasingly complex, the solutions remain embarrassingly simple.” ― Bill Mollison