Hi I am looking for ways to delete the elements from associate array.
I need to remove values like null and '' while in the loop. But I cant because I know that I will need to identify and the build array to store, Then use elements in the new array to seek and remove them.
var storeData3 = [
{ 'key' : 'value1' },
{ 'key' : 'value2' },
{ 'key' : 'value3' },
{ 'key' : null },
{ 'key' : '' },
{ 'key' : 'value10'}
];
Try this:-
Using Array.filter to get the data after omitting unwanted data.
var result= storeData3
.filter(function(val){
return (val.key != '' && val.key != null)});
.filter()
Fiddle
Related
I have an object that looks like this:
{
"examples":[
{
"key":"value1"
},
{
"key":"value1",
"key2":"example"
},
{
"key":"value1"
},
{
"key":"value2"
},
{
"key":"value2",
"key2":"example"
},
{
"key":"value2"
}
]
}
I'm trying to use findIndex to find where in the object that key === 'value1' and key2 exists (so in this instance the index would be 1).
I've tried using something like var x = examples.findIndex(({key}) => key === 'value1' && ({key2}) => key2) but it's not working. How do I go about this? Any answers would be greatly appreciated! :)
examples is a property of the data object, so you need to iterate over that array. examples.findIndex won't do anything.
You can then check to see if key has "value1" as a value, and that there is a key2 in the object.
const data={examples:[{key:"value1"},{key:"value1",key2:"example"},{key:"value1"},{key:"value2"},{key:"value2",key2:"example"},{key:"value2"}]};
const result = data.examples.findIndex(obj => {
return obj.key === 'value1' && obj.key2;
});
console.log(result);
I have an response as a json array with lots of records but i want to filter that json array by using another json array.
my json response
http://prntscr.com/lvxwob
and my filter json array be like
"filterParams" : [
{
"param" : "actualSum",
"value" : "95",
"type" : "text",
"comparision" : "isEqual"
},
{
"param" : "wbsSort",
"value" : "6",
"type" : "text",
"comparision" : "isEqual"
}
],
so how can i filter my response using javascript or node js anything. i want to get filtered data like match param with reponse param and its value.
e.g.
if there is match value of actualSum with 95 and wbsSort's value equal to 6 then it will return true other wise false.
You could filter the items in the result array where the item matches every parameter in filterParams. If you only want to check if at least one match exists replace .filter with .some
e.g.
var matches = results.filter(item =>
filterParams.every(paramItem =>
item[paramItem.param] === paramItem.value));
I've limited it to equals comparison but you can expand the comparison using a switch based on the other comparison types you have.
A way to do this without having to hardcode every check would be to create a compare function using the filterParams. So you would do something like this, where compareFunction creates a new function using filterParams to initialize the variables to check and returns whether the current item has these values. So for any further check you want to do, you will only have to add it to filterParams. Hope this helps.
const filterParams = [{
"param" : "actualSum",
"value" : "95",
"type" : "text",
"comparison" : "isEqual"
}, {
"param" : "wbsSort",
"value" : "6",
"type" : "text",
"comparison" : "isEqual"
}];
const data = [{ actualSum: 95, wbsSort: 6 }, { actualSum: 95, wbsSort: 10 }];
const operators = {
'isEqual': '==='
};
const compareFunction = (params) =>
(item) => new Function(
params.reduce((acc, { param, value }) => `${acc} const ${param} = ${value};`, '') +
params.reduce((acc, { param, value, comparison }, i) => `${acc} ${param} ${operators[comparison]} ${item[param]} ${i !== params.length - 1 ? '&&' : ';'}`, 'return ')
)();
const filteredData = data.filter(compareFunction(filterParams));
console.log(filteredData);
I want to send a data attribute to an API server only if the method is add, if it's delete, I don't don't want to send my data.
I have
var body =
{
id: 1,
method: method,
params: [
{
data: {
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
},
url: `/url/anything`
}
],
session: session,
verbose: 1
};
I tried
if(method == 'delete') {
_.pick(body.params[0], ['data']);
}
I also tried
if(method == 'delete') {
_.pick(body.params[0],'data');
}
For some reason, I still see that I still sending the data.
How would one go about debugging this?
if you take a look at lodash pick documentation you'll see that it doesn't change the source object instead its create a new object from the source object properties and returns the new object , if you want to remove the data property from the source object , you can use the unset method from lodash which removes a property from an object or a set of props ,
also you can use the delete operator
use _.assign:
var body =
{
id: 1,
method,
params: [
_.assign({url: `/url/anything`}, method === 'delete'
? null
: {
data: {
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
}
}
)
],
session,
verbose: 1
};
You may need to do this.
if(method === 'delete') {
body.params[0].data = undefined;
}
In this case, we delete all of the content of data by assign undefined.
Can anyone tell me what how to update 'value' by id in this structue in ImmutableJS?
map = {
list: [
{
id: 1,
value: 'update'
}
]
}
You can use the code below. However, it is not the best solution, because that will search all items in the list. If you can change your structure instead of List you can use OrderedMap.
var newData = map.set("list", map.get("list").map(data => data.get("id") === 1 ? data.set("value", "updatedValue") : data));
I have a predefined object (SampleObject) like this:
{
ID: "",
Name: "",
URL: "",
prevName: "",
Code: "",
}
And I want to insert the below json object values(values only):
var object =
{
"Sample" : {
"Data" : {
"ID" : "12345",
"Name" : "SampleName: Name",
"URL" : "www.google.com",
"prevName" : "phones",
"Code" : "USD"
}
}
into the above predefined object. How do I do that?
You can just use a for in loop and set the value checking if the key is present in the object or not.
Check if the property is present on the emptyObject, and then copy that over to it.
for (var key in pageInfo) {
var value = pageInfo[key];
if (obj.hasOwnProperty(key)) {
obj[key] = value;
}
}
Code Pen
It is an object. There is no reason to use push or another method.
Simply take your defined object pageObject.page and assign a new key value pair with literal syntax.
pageObject.page['pageInfo'] = predefinedObject
or in more common syntax
pageObject.page.pageInfo = predefinedObject
Use the following code below the JSON Object
var digitalData= {page:{pageInfo:''}};
digitalData.page.pageInfo = pageObject.page.pageInfo;
console.log(digitalData.page.pageInfo);