Need to remove object from javascript by key - javascript

I have an array:
(2) [{…}, {…}]
0: {code: "sku", label: "SKU", value: "Number: 312312"}
1: {code: "show_more_options", label: "Show More Options", value: "New variant!"}
length: 2
__proto__: Array(0)
getting from variable order.attributes_list.
i need to remove from that element that has code === "sku".

You can use .filter for this with destructuring assignment to build a new array of wanted objects/items. Here,
if the inner arrow function returns true the item (ie object) will be kept within the new array, if it returns false it wont be added.
const arr = [{code: "sku", label: "SKU", value: "Number: 312312"},
{code: "show_more_options", label: "Show More Options", value: "New variant!"}];
const searchCode = "sku";
const res = arr.filter(({code}) => code !== searchCode);
console.log(res);

Related

ReactJS Convert my strange array to an object to add it to a list

I have the following array:
[{…}]
0: {id: 2, createdAt: "2021-06-11T10:13:46.814Z", exchangedAt: "2021-06-11T08:04:11.415Z", imageUrl: "url", user: "user", …}
1: ....
2: ....
3: ....
....
length: 5
__proto__: Array(0)
I want to convert it to an object and I found this code, but all I receive is the 0:
Object.keys(fetchedData.data).map(key => console.log(key))
How to access and convert my 5 array values?
So I've iterated across your array and used the 'id' field as a key. The key for each object needs to be unique, so this assumes your ID is unique...
const fetchedData = [
{id: 1, createdAt: "2021-06-11T10:13:46.814Z", exchangedAt: "2021-06-11T08:04:11.415Z", imageUrl: "url", user: "user"},
{id: 2, createdAt: "2021-06-11T10:13:46.814Z", exchangedAt: "2021-06-11T08:04:11.415Z", imageUrl: "url", user: "user"},
{id: 3, createdAt: "2021-06-11T10:13:46.814Z", exchangedAt: "2021-06-11T08:04:11.415Z", imageUrl: "url", user: "user"}
]
obj = {}
fetchedData.forEach(x => {
tempX = {...x}
delete tempX["id"]
obj[x.id] = tempX
})
console.log('Converted object', obj)
//Using your method, just to log the values without converting it into an object, you could do...
fetchedData.forEach(el => console.log("Individual Element", el))
...so when you use Object.keys() you are iterating across the keys of an object, but as this is an array of objects, the keys are just the indexes of the array. .forEach iterates across the array and gives you each element to work with.
You've used .map() in your code, which also iterates through an array, but returns a new array, so you could change each element. .forEach just iterates and doesn't return, so you're better off using this for logging to the console.
a = [
{
"id":1,
"createdAt":"2021-06-11T10:13:46.814Z",
"exchangedAt":"2021-06-11T08:04:11.415Z",
"imageUrl":"url",
"user":"user"
},
{
"id":2,
"createdAt":"2021-06-11T10:13:46.814Z",
"exchangedAt":"2021-06-11T08:04:11.415Z",
"imageUrl":"url",
"user":"user"
},
{
"id":3,
"createdAt":"2021-06-11T10:13:46.814Z",
"exchangedAt":"2021-06-11T08:04:11.415Z",
"imageUrl":"url",
"user":"user"
},
{
"id":2,
"createdAt":"2021-06-11T10:13:46.814Z",
"exchangedAt":"2021-06-11T08:04:11.415Z",
"imageUrl":"url",
"user":"user"
},
{
"id":4,
"createdAt":"2021-06-11T10:13:46.814Z",
"exchangedAt":"2021-06-11T08:04:11.415Z",
"imageUrl":"url",
"user":"user"
}
]
(5) [{…}, {…}, {…}, {…}, {…}]
Object.keys(a).map(key => console.log(key))
VM532:1 0
VM532:1 1
VM532:1 2
VM532:1 3
VM532:1 4
It is accessing to 5 values.

Filtering in Object of Arrays in Javascript

I am trying to filter out an Object of Arrays wherein I have to filter out the 'designation' 'options' of a key.
My row Object looks like.
{id: 1, columns: Array(5)}
columns: (5) [InputDate, InputSelect, InputSelect, InputSelect, InputString]
id: 1
__proto__: Object
One of the columns Array where data is looks like this.
InputSelect
name: "designation"
options: Array(3)
0: {name: "Entry", value: "ENTRY"}
1: {name: "Mid", value: "MID"}
2: {name: "Experienced", value: "EXPERIENCE"}
length: 3
__proto__: Array(0)
Now I have to search inside the columns Arrays and filter out the options of "ENTRY" if the Arrays name property is 'designation' so as to return the updated Object having this filtered value.
What I am trying now is
row.columns.map( key => {
if (key.name === 'designation') {
key.options.filter( r => r.value === "ENTRY" )
}
But it doesn't seem to be updating the row Object.
This should solve your problem.
row.columns.filter(x=>x.name=="designation").map(i=>i.options.filter(z=>z.name==="Entry"));

React - for loop only returns index and not whole object

I have a component which has the following object ('items') in its state:
items:
count: 2
next: null
previous: null
results: Array[2]
0: {…}
id: 1
price: "100.00"
show_in_shop: true
tax_percentage: "5.0000000"
title: 'Object title'
type: "assessment"
1: {…}
id: 2
price: "1000.00"
show_in_shop: true
tax_percentage:"8.0000000"
title: "Title of this object"
type: "class"
I am creating a filter that loops over this.state.items.results and checks if the values of the Item object are in the search query.
However, I am running into the following problem, when I run this code:
for (let item in this.state.items.results) {
console.log(item);
What gets printed to the console is
0
1
And not:
0: {…}
id: 1
price: "100.00"
show_in_shop: true
tax_percentage: "5.0000000"
title: 'Object title'
type: "assessment"
1: {…}
id: 2
price: "1000.00"
show_in_shop: true
tax_percentage:"8.0000000"
title: "Title of this object"
type: "class"
But why? When I check the state in the react-chrome plugin I see that the 'item.results' is populated with data and contains more data than is being printed to the console. Does anyone know why the loop is only looping over the index and not returning the object?
javascript for .. in indeed only returns the index.
Use:
for (let key in this.state.items.results) {
let item = this.state.items.results[key];
console.log(item);
}
use the for-of
for (let item of this.state.items.results) {
console.log(item);
}
this.state.items.result.forEach(item => console.log(item));
for...in will gives the index only, if you want the each object then use for...of.
Like this:
var arr = [{a:1}, {a:2}];
for(let el of arr){
console.log(el);
}
For the looping you can use #array.forEach or #array.map also, depending upon the requirement.
for...in will return index of the collection and not the item of the collection.
For eg:
var array = [10,20,30];
for(var item in array){
console.log(array[item]);
}

Distinct Values using Set

I have been using
<FormGroup>
<Campaign
campaignName={this.campaignName.bind(this)}
campaignOptions={[...new Set(this.props.records.map(options => options.campaign))]}/>
</FormGroup>
to get a unique array for a dropdown option, however I now need to pass an array with each object in it having a label and a value.
I have refactored the code as follows,
<FormGroup>
<Campaign
campaignName={this.campaignName.bind(this)}
campaignOptions={[...new Set(this.props.records.map(options => {
return {value: options.campaign, label: options.campaign };
}))]}/>
</FormGroup>
...but the unique element is now not working (see below with duplicates). What am I doing wrong please?
The input array looks like...
[
1. 0:{_id: "zWTiLEjqrCKdxTvze", owner: "K7xTxu7PRDCuhFZRC", company_ID: "1", campaign: "newevent", …}
2. 1:{_id: "SkZzDMAWokFFYEycp", owner: "K7xTxu7PRDCuhFZRC", company_ID: "1", campaign: "instagramlaunch", …}
3. 2:{_id: "p4pychanC5Zw8iWAQ", owner: "K7xTxu7PRDCuhFZRC", company_ID: "1", campaign: "stuffinhere", …}
… more in here including more than 1 object with instagram launch for example
]
Original approach resulted in Array of...
0:"newevent"
1:"sheffieldevent"
2:"stuffinhere"
3:”instagramlaunch"
4:”anothertest"
5:”yetanother"
now results in...
0:{value: "newevent", label: "newevent"}
1:{value: "sheffieldevent", label: "sheffieldevent"}
2:{value: "stuffinhere", label: "stuffinhere"}
3:{value: "instagramlaunch", label: "instagramlaunch"}
4:{value: "instagramlaunch", label: "instagramlaunch"}
5:{value: "instagramlaunch", label: "instagramlaunch”}
6:{value: "anothertest", label: "anothertest”}
7:{value: "yetanother", label: "yetanother"}
new Set(this.props.records.map(options => {
return {value: options.campaign, label: options.campaign };
}))
Does not work, because every new object is a unique object and Set works with all types. As result, you get all options, but not unique options.
If you have the same pattern as you want, you could render the set after collecting all values and build a new objects array out of the set.
var optionsSet = new Set(array.map(option => option.campaign)),
options = Array.from(optionsSet, v => ({ value: v, label: v }));
I resolved this using lodash...
campaignOptions={_.uniqBy(this.props.records.map((options,index) => {return { value: index, label: options.campaign }}),'label')}/>

Foreach loop inside array/object

I don't have a good understanding of Javascript, so I don't know if I even formulated my question properly.
If I have an array with objects, how do I populate and replace this 'values' with mydata values? I need some sort of foreach loop, I don't know how many objects there will be.
editor.addButton( 'gavickpro_tc_button', {
fixedWidth: true,
values: [
{
text: mydata.knjiga,
value: mydata.shortcode,
onclick: function() {
editor.insertContent(this.value());
}
},
{
text: mydata.knjiga2,
value: mydata.shortcode2,
onclick: function() {
editor.insertContent(this.value());
}
}
{
text: mydata.knjiga3,
value: mydata.shortcode3,
onclick: function() {
editor.insertContent(this.value());
}
}
]
});
mydata looks like this
[Object, Object, Object]
0: Object
id: 10
knjiga: "Something 1"
shortcode: "some value 123"
__proto__: Object
1: Object
id: 12
knjiga: "Something 3"
shortcode: "some value 12345"
__proto__: Object
2: Object
id: 13
knjiga: "Something 3"
shortcode: "some value 1235678"
__proto__: Object
length: 3
__proto__: Array[0]
Raw JavaScript doesn't have foreach loop: http://www.w3schools.com/js/js_loop_for.asp. To iterate over each element in array you can use for loop as below:
for (i = 0; i < objectArray.length; i++) {
object= objectArray[i];
object.knjiga = "Best book";
}
In jQuery is function each: http://api.jquery.com/jquery.each/ and realize the foreach loop.

Categories

Resources