Popular Posts

Saturday, May 20, 2017

JS Dump from VS Code:




function abc(id) {

var countriesSubscription = [
{ Exchange: 'ASX', CountryId: 1, ExchangeRate: 1, CountryName: "Australia", Code: "AU", CurrencyForeign: "AUD", CurrencyLocal: "AUD", IsSubscribed: true, PriceLocal: 15.75, PriceForeign: null, DataType: "ClickToRefresh" },
{ Exchange: 'ASX', CountryId: 2, ExchangeRate: 1.01, CountryName: "Canada", Code: "CAN", CurrencyForeign: "CAD", CurrencyLocal: "AUD", IsSubscribed: false, PriceLocal: null, PriceForeign: null, DataType: "" },
{ Exchange: 'ASX', CountryId: 3, ExchangeRate: 5.78, CountryName: "Hong Kong", Code: "HKG", CurrencyForeign: "HKD", CurrencyLocal: "AUD", IsSubscribed: false, PriceLocal: null, PriceForeign: null, DataType: "" },
{ Exchange: 'CHIX', CountryId: 4, ExchangeRate: 0.74, CountryName: "US", Code: "USA", CurrencyForeign: "USD", CurrencyLocal: "AUD", IsSubscribed: true, PriceLocal: null, PriceForeign: null, DataType: "" },
{ Exchange: 'ASX', CountryId: 5, ExchangeRate: 0.67, CountryName: "France", Code: "FRA", CurrencyForeign: "EUR", CurrencyLocal: "AUD", IsSubscribed: false, PriceLocal: null, PriceForeign: null, DataType: "" },
{ Exchange: 'CHIX', CountryId: 6, ExchangeRate: 1.37, CountryName: "Switzerland", Code: "CHE", CurrencyForeign: "CHF", CurrencyLocal: "AUD", IsSubscribed: true, PriceLocal: null, PriceForeign: null, DataType: "" },
{ Exchange: 'ASX', CountryId: 7, ExchangeRate: 0.73, CountryName: "Belgium", Code: "BEL", CurrencyForeign: "EUR", CurrencyLocal: "AUD", IsSubscribed: false, PriceLocal: null, PriceForeign: null, DataType: "" },
];

countriesSubscription.forEach(function (element) {
if (element.CountryId === id) {
element.IsSubscribed = true;
return;
}
});

for (var index = 0; index < countriesSubscription.length; index++) {
var element = countriesSubscription[index];
console.log(element.CountryName + ' ' + element.IsSubscribed);
}

// Use map function
var result = countriesSubscription.map(function (element) {
return element.CurrencyLocal + " " + element.PriceLocal;
});

result.forEach(function (element) {
console.log(element);
}, this);

// reduce array of objects to dictionary
var countryDictionary = countriesSubscription.reduce(function (o, v) {
o[v.CountryId] = v;
return o;
}, {});

console.log(countryDictionary[1]);
console.log(countryDictionary[1].CountryName);
console.log(countryDictionary[2]);
console.log(countryDictionary[2].CountryName);

// Find index of element
Array.prototype.indexOfObject = function countriesSubscription(property, value) {
for (var i = 0, len = this.length; i < len; i++) {
if (this[i][property] === value) return i;
}
return -1;
}

var result = countriesSubscription.indexOfObject("CountryName", "Canada");
console.log(result);

result = countriesSubscription.map(function (e) { return e.CountryName; }).indexOf('Canada');
console.log(result);
console.log(countriesSubscription[result].CountryName);

// **** Array to group by Exchange ****
var groupByCurrency = countriesSubscription.reduce(function(all, item, index){
all[item.Exchange].push(item);
return all;
}, {CHIX:[], ASX:[]});

groupByCurrency.ASX.forEach(function(item){
console.log(item);
})

groupByCurrency.CHIX.forEach(function(item){
console.log(item);
})

// Custom function that loops and finds

countrySearch = function (array, callback) {
for (var key in countriesSubscription) {
if (callback(countriesSubscription[key])) {
return countriesSubscription[key];
}
}
}

var countryById = this.countrySearch(countriesSubscription, function(key){
return key.CountryId === 1;
})
console.log(countryById);

var countryByName = this.countrySearch(countriesSubscription, function(key){
return key.CountryName === "Australia";
})
console.log(countryByName);

var CountryBySubscribtion = this.countrySearch(countriesSubscription, s => s.IsSubscribed === false )
console.log(CountryBySubscribtion.CountryName);
}

abc(3);

No comments:

Post a Comment