Related
I'm quite new to Javascript and I have the following javascript array in an AJAX Request that contains the following:
["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"]
I wanna manipulate the TRUE and FALSE value. If they're in uppercase, I want to make it lowercase. Any idea how I can do it?
If you want to modify the list you could just loop through all of its items, modify the value and set it to the same index of the list. (You don't need to set it if you are dealing with objects).
var list = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"];
list.forEach(function(item, index) {
list[index] = item.replace(/(TRUE|FALSE)/g, function(upperCase) {
return upperCase.toLowerCase();
});
});
console.log(list);
Same thing using a for loop:
var list = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"];
for (var index = 0; index < list.length; index++) {
list[index] = list[index].replace(/(TRUE|FALSE)/g, function(upperCase) {
return upperCase.toLowerCase();
});
}
console.log(list);
If you want to create a copy you could do:
var list = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"];
var newList = list.map(function(item) {
return item.replace(/(TRUE|FALSE)/g, function(upperCase) {
return upperCase.toLowerCase();
});
});
console.log(newList);
The above scripts will also transform something like ["THIS IS NOT TRUE|TRUE|FALSE"] to ["THIS IS NOT true|true|false"]. If you do not want that you should use this regex instead /(^|(?<=\|))(TRUE|FALSE)(\||$)/ i.e.:
var list = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRowFALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"];
for (var index = 0; index < list.length; index++) {
list[index] = list[index].replace(/(^|(?<=\|))(TRUE|FALSE)(\||$)/g, function(upperCase) {
return upperCase.toLowerCase();
});
}
console.log(list);
Just use replace with map:
const arr = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"];
const res = arr.map(e => e.replace(/(TRUE|FALSE)/g, m => m.toLowerCase()));
console.log(res);
const arrayString = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"]
const arrayOfValues = arrayString[0].split('|').map(val => {
if(val === 'TRUE' || val === 'FALSE') {
return val.toLowerCase();
} else {
return val;
}
});
console.log(arrayOfValues)
Use RegEx as you have been told before.
If you want to learn more about this look at: W3Schools
One solution could be like this:
var ajaxResponse = "12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE";
ajaxResponse = ajaxResponse.replace(/FALSE/g, "false");
ajaxResponse = ajaxResponse.replace(/TRUE/g, "true");
console.log(ajaxResponse);
I am trying to search the below array which is in JSON format.
[{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"807"},
{"SystemID":"62750003","ApparentPower":"796"}]
I want to first check the first element in this case "SystemID" and append all the values of "SystemID" to op1 array I have created. I am not sure how to do this, my code to search the array is below (JS file):
$(document).ready(function() {
$.ajax({
url: "http://localhost/chartjs/data.php",
method: "GET",
success: function(data) {
op1 = [];
if (data[i] == 'SystemID') {
for(var i in data) {
op1.push(data[i]['SystemID'])
}
}
}
}
}
When I run this code now, op1 is empty.
I want op1 to have all the values of SystemID from the JSON array.
UPDATE: I want to check IF the element is "SystemID" and if so, appened the first element to "op1". The first element and second element in my JSON data could change, so I want to check that first and then append the first element to "op1". Also I want to check the second element, and append the second elements value to "op2" array.
As i understand you want all the SystemID in the array op1:
const data = [{"SystemID":"62750003","ApparentPower":"822"},{"SystemID":"62750003","ApparentPower":"822"}, {"SystemID":"62750003","ApparentPower":"807"},{"SystemID":"62750003","ApparentPower":"796"}];
const op1 = data.map(item => item.SystemID);
console.log(op1);
You need something like that , you are missing the for loop
var data = [{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"807"},
{"SystemID":"62750003","ApparentPower":"796"}]
var op1 = [];
var op2 = [];
for(let i=0; i < data.length;i++){
for(let item in data[i]){
if(item == "SystemID"){
op1.push(data[i][item])
}
if(item == "ApparentPower"){
op2.push(data[i][item])
}
}
}
console.log(op1)
console.log(op2)
if you wanna use ECMA6
let data = [{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"807"},
{"SystemID":"62750003","ApparentPower":"796"}]
let op1 = data.map(item => item.SystemID);
let op2 = data.map(item => item.ApparentPower);
console.log(op1)
console.log(op2)
if you wanna global concept
let data = [{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"807"},
{"SystemID":"62750003","ApparentPower":"796"}]
let data_keys = Object.keys(data[0])
let result = []
for(let item in data_keys){
result.push([])
}
for(let i=0; i < data.length;i++){
for(key in data_keys){
if(Object.keys(data[i])[key] == data_keys[key]){
result[key].push(Object.values(data[i])[key])
}
}
}
for(let k=0; k<result.length;k++){
console.log(result[k])
}
console.log(result)
You want to check if the first key in particular is SystemID ? if that's the case you could try
const data = [
{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"807"},
{"SystemID":"62750003","ApparentPower":"796"},
{"NOTASystemID":"6sss","ApparentPower":"796"}
];
const op = data
.filter(element => Object.keys(element)[0]==="SystemID") //filtering out element with first key sysID
.map(element => element.SystemID);
console.log(op);
As per my understanding your code will be like this
var json = [{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"822"},
{"SystemID":"62750003","ApparentPower":"807"},
{"SystemID":"62750003","ApparentPower":"796"}];
var myArray =[];
json.forEach(function (item) {
if(item.SystemID) {
myArray.push(item.SystemID);
}
});
console.log(myArray);
Try This,
var data = '[{"SystemID":"62750003","ApparentPower":"822"},{"SystemID":"62750003","ApparentPower":"822"},{"SystemID":"62750003","ApparentPower":"807"},{"SystemID":"62750003","ApparentPower":"796"}]';
var op1 = [];
$.each(JSON.parse(data),function(i,item){
op1[i] = item.SystemID;
});
console.log(op1);
Assuming you have already parsed the JSON data, you can just map through the array and use the hasOwnProperty() method to check if each object in the array has SystemID or not and if it does, push it's value to the op1 array.
Trying to retrieve elements in an object using index is not reliable since object properties are unsorted.
var data = [{"SystemID":"62750003","ApparentPower":"822"},{"SystemID":"62750003","ApparentPower":"822"},{"SystemID":"62750003","ApparentPower":"807"},{"SystemID":"62750003","ApparentPower":"796"}];
var op1 = data.map(e => {
if (e.hasOwnProperty("SystemID")) {
return e.SystemID;
} else {
// do something else since current mapped object doesn't have "SystemID"
}
});
console.log(op1);
I was having some problem when trying to check for duplicate by multiple fields before inserting into the array. What I am trying to do is retrieve from firebase, check for accountID and subtype fields before inserting into the array to be resolved by Promise.
What I trying to achieve is If same accountID, different subtype, then I add; If same accountID, same subtype, I move to next; If different accountID, different subtype, I add. Here is my code:
code:
var datasetarr = [];
let promiseKey = new Promise((resolve, reject) => {
for(var i = 0; i < receiptlist.length; i++){
for(var k = 0; k < ritemlist.length; k++){
if(receiptlist[i].date.substring(0, 4) == new Date().getFullYear()){
if(ritemlist[k].receiptID == receiptlist[i].receiptID){
//check duplicate here before insert
if (!datasetarr.find(o => o.accountID === receiptlist[i].accountID && o.subtype === ritemlist[k].type))
datasetarr.push({accountID: receiptlist[i].accountID, subtype: ritemlist[k].type});
}
}
}
}
}
resolve(datasetarr);
});
The part when I tried to print out the array:
array:
promiseKey.then((arr) => {
console.log(arr);
});
The output I am getting:
output:
I still see a lot of duplicate with same accountID and same subtype. Is there anyway to resolve this?
Thanks!
find return undefined if there is no occurrence of your data; so what you have to do is to check whether the value returned is undefined and then you do your computation
var found = datasetarr.find(o => o.accountID === receiptlist[i].accountID && o.subtype === ritemlist[k].type)
if (found === undefined){
//do computation
}
You can use lodash which is very good library for array handling.
in your case data should be unique by accountId and your data is stored in data variable, you can use _.uniqBy() function like this:
jvar datasetarr = [];
let promiseKey = new Promise((resolve, reject) => {
for(var i = 0; i < receiptlist.length; i++){
for(var k = 0; k < ritemlist.length; k++){
if(receiptlist[i].date.substring(0, 4) == new
Date().getFullYear()){
if(ritemlist[k].receiptID == receiptlist[i].receiptID){
//Push your object here.
datasetarr.push({accountID: receiptlist[i].accountID, subtype: ritemlist[k].type});
}
}
}
}
}
//Before resolving check for the duplicates.
_.uniqBy(datasetarr, function (data) {
return data.accountID;
});
resolve(datasetarr);
});
let arr = ["Apple", "Orange"]
let newElement = "Apple"
if (!arr.includes(newElement)) arr.push(newElement)
else console.log("Element is already there")
console.log("Array Elements : " + arr)
Looking for other suitable methods for the same functionality.
If I have an array like this:
var array = [{ID:1,value:'test1'},
{ID:3,value:'test3'},
{ID:2,value:'test2'}]
I want to select an index by the ID.
i.e, I want to somehow select ID:3, and get {ID:3,value:'test3'}.
What is the fastest and most lightweight way to do this?
Use array.filter:
var results = array.filter(function(x) { return x.ID == 3 });
It returns an array, so to get the object itself, you'd need [0] (if you're sure the object exists):
var result = array.filter(function(x) { return x.ID == 3 })[0];
Or else some kind of helper function:
function getById(id) {
var results = array.filter(function(x) { return x.ID == id });
return (results.length > 0 ? results[0] : null);
}
var result = getById(3);
With lodash you can use find with pluck-style input:
_.find(result, {ID: 3})
Using filter is not the fastest way because filter will always iterate through the entire array even if element being search for is the first element. This can perform poorly on larger arrays.
If you are looking for fastest way, simply looping through until the element is found might be best option. Something like below.
var findElement = function (array, inputId) {
for (var i = array.length - 1; i >= 0; i--) {
if (array[i].ID === inputId) {
return array[i];
}
}
};
findElement(array, 3);
I would go for something like this:
function arrayObjectIndexOf(myArray, property, searchTerm) {
for (var i = 0, len = myArray.length; i < len; i++) {
if (myArray[i].property === searchTerm)
return myArray[i];
}
return -1;
}
In your case you should do:
arrayObjectIndexOf(array, id, 3);
var indexBy = function(array, property) {
var results = {};
(array||[]).forEach(function(object) {
results[object[property]] = object;
});
return results
};
which lets you var indexed = indexBy(array, "ID");
I have the object below that is used by datatables, I want to know how to remove items by name.
Example
Lets say I want to remove sEcho, mDataProp_1 and sSearch from the object below, would the best way to loop through all items and check the name or is there a easier way.
[{"name":"sEcho","value":1},{"name":"iColumns","value":9},
{"name":"sColumns","value":""},{"name":"iDisplayStart","value":0},
{"name":"iDisplayLength","value":10},{"name":"mDataProp_0","value":0},
{"name":"mDataProp_1","value":1},{"name":"mDataProp_2","value":2},
{"name":"mDataProp_3","value":3},{"name":"mDataProp_4","value":4},
{"name":"mDataProp_5","value":5},{"name":"mDataProp_6","value":6},
{"name":"mDataProp_7","value":7},{"name":"mDataProp_8","value":8},
{"name":"sSearch","value":""},{"name":"bRegex","value":false},
{"name":"sSearch_0","value":""},{"name":"bRegex_0","value":false},
{"name":"bSearchable_0","value":false},{"name":"sSearch_1","value":""},
{"name":"bRegex_1","value":false},{"name":"bSearchable_1","value":false},
{"name":"sSearch_2","value":""},{"name":"bRegex_2","value":false}]
Examples would be great.
Thanks
Here is a little jsfiddle that do just that http://jsfiddle.net/wHkTS/
The idea is to iterate over the area and compare the name you want to remove with the currently iterate object name and basically build a new array to assign back that doesn't contain the object you want to remove.
var data = [
{"name":"sEcho","value":1},{"name":"iColumns","value":9},
{"name":"sColumns","value":""},{"name":"iDisplayStart","value":0},
{"name":"iDisplayLength","value":10},{"name":"mDataProp_0","value":0},
{"name":"mDataProp_1","value":1},{"name":"mDataProp_2","value":2},
{"name":"mDataProp_3","value":3},{"name":"mDataProp_4","value":4},
{"name":"mDataProp_5","value":5},{"name":"mDataProp_6","value":6},
{"name":"mDataProp_7","value":7},{"name":"mDataProp_8","value":8},
{"name":"sSearch","value":""},{"name":"bRegex","value":false},
{"name":"sSearch_0","value":""},{"name":"bRegex_0","value":false},
{"name":"bSearchable_0","value":false},{"name":"sSearch_1","value":""},
{"name":"bRegex_1","value":false},{"name":"bSearchable_1","value":false},
{"name":"sSearch_2","value":""},{"name":"bRegex_2","value":false}
];
function remove(name) {
var arr = [], len, i;
// we reset len as data.length will change after erach remove
for(i = 0, len = data.length; i < len; i++) {
if (data[i].name != name) arr.push(data[i]);
};
data = arr;
};
console.log(data);
remove('sEcho');
console.log(data);
The modern ES5 way is Array.filter:
var original = [{"name":"sEcho","value":1}, ... ];
var filtered = original.filter(function(val, index, array) {
var n = val.name;
return n !== 'sEcho' && n !== 'mDataProp_1' && n !== 'sSearch';
});
I think you'll need to create a function that can search and then remove them, something like
function deleteByName(needle, haystack) {
for(i in haystack) {
if ( haystack[i].name == needle) {
haystack.splice(i,1);
}
}