how to parse array values inside object [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
{ 'notes[]': [ 'book', 'copy', 'pencil', 'eraser' ] }
I just want to parse these sets. Please, anyone suggest me some best approach

Since you are using an invalid identifier as your key (notes[]), you will need to use bracket syntax to access:
var data = { 'notes[]': [ 'book', 'copy', 'pencil', 'eraser' ] };
console.log(data['notes[]']);
Probably would be better to name the key notes instead of notes[]. Then you can access with dot notation: data.notes

Maybe you want like as below code.
var res = { 'notes': [ 'book', 'copy', 'pencil', 'eraser' ] };
console.log(res.notes[0]); //=> book
console.log(res.notes[1]); //=> copy
console.log(res.notes[2]); //=> pencil
console.log(res.notes[3]); //=> eraser

Related

How can I parse a stringified array of arrays (JavaScript)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I have a string which represents an array. The items in the array can be characters, or arrays. So a string might look like: [a,b,[[],[d]],[],[[[]]],[[c,[t,s]],b]]
And I want to parse that out so it's a proper array with subarrays etc
So "[a,b,[c,[d,e],[]]]" would become:
[ 'a',
'b',
[ 'c',
['d','e'],
[]
]
]
Is there an easy way to do this in JavaScript? Eg some kind of array equivalent of JSON.parse? I tried JSON.parse but it throws an error (not unexpectedly).
You would have to process it to convert it into a format that JSON.parse would be able to handle. Your test case is simple so it is possible with an easy regular expression.
const str = "[a,b,[c,[d,e],[]]]";
const obj = JSON.parse(str.replace(/([a-z]+)/gi,'"$1"'));
console.log(obj);

Combine two JSON then add title [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have two JSON objects and I'm planning to join them.
obj1 = [{'foo': 'bar', 'foo1': 'bar1'}];
obj2 = [{'foo2': 'bar2'}];
objFinal = [
{
"foo": "bar",
"foo1": "bar1",
"title":
[
{ "foo2": "bar2" }
]
}
]
My plan is to get the result the same as objFinal. I've used .concat but it didn't returned the result I wanted. What are other options I can do to get the result as same as objFinal?
Please use this code.
objFinal = [{ ...obj1[0], title: obj2}];

dot character in property name [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I got this code snippet from jest documentation.
In the example, one of the variables is named ceiling.height and is the only variable with a dot . in the name. What would be the purpose of using variables with a dot in the name such that it warrants this example in the documentation?
const houseForSale = {
bath: true,
bedrooms: 4,
kitchen: {
amenities: ['oven', 'stove', 'washer'],
area: 20,
wallColor: 'white',
'nice.oven': true,
},
'ceiling.height': 2,
};
The purpose would be to store nested objects in a flat format, ie. a database table.
You could use something like dottie.js to transform this flat object into a nested object
const values = {
'user.name': 'Gummy Bear',
'user.email': 'gummybear#candymountain.com',
'user.professional.title': 'King',
'user.professional.employer': 'Candy Mountain'
};
const transformed = dottie.transform(values);
We would now find this to be the value of transformed
{
user: {
name: 'Gummy Bear',
email: 'gummybear#candymountain.com',
professional: {
title: 'King',
employer: 'Candy Mountain'
}
}
}
Why they use it like that in the example seems to be so that they can provide an example (below) on how to access these kinds of variable names. Imagine that you wanted the .toHaveProperty(keyPath value?) function from the Jest docs to operate on a property in the values object from the above example. This shows you how you might use this function with such a property.
// Referencing keys with dot in the key itself
expect(houseForSale).toHaveProperty(['ceiling.height'], 'tall');
Unless you have a very good reason, using these kinds of variable names is not a best practice and I would not recommend it. That said, while a variable name with a . character is not able to be used with dot syntax:
houseForSale.ceiling.height = undefined
It is still accessible with bracket syntax:
houseForSale['ceiling.height'] = 2

Underscore.js: Combining two arrays with objects using a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have 2 arrays with objects:
first = {
a:false,
b:false,
c:false,
d:false
}
second = {
a:true,
c:true
};
I want to get:
third = {
a:true,
b:false,
c:true,
d:false
}
I'm using underscore and jQuery.
I tried _.union but it returns an array with all the indexes of a&b.
It seems like your're looking for underscore's defaults function
var third = _.defaults(second, first);
// Object {a: true, b: false, c: true, d: false}

Find key by value in JS object [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have following piece of code:
var search_value = 'XYZ';
var FIELD_MAP = {
'key1': [
'SOME',
'THING'
],
'key2': [
'ANOTHER_VALUE',
'XYZ'
]
};
I need to find which array (key1, key2 etc) has a value stored in variable search_value. What is the best way to do this?
This can be achieved by iterating over the properties of FIELD_MAP and checking if the required search string is available in any of them:
for (var key in FIELD_MAP) {
if (FIELD_MAP.hasOwnProperty(key)) {
if (FIELD_MAP[key].indexOf(search_value) > 0) {
console.log("Found in=",key)
}
}
}
You can do it this way by filtering keys on FIELD_MAP object by checking if the value contains the search_value by using Object.keys to get keys and Array.filter to filter them.
var key = Object.keys(FIELD_MAP).filter(function(k){
return ~FIELD_MAP[k].indexOf(search_value);
})[0]; // remove the [0] if you want multiple keys matching the criteria

Categories

Resources