Create object array according to changing number of properties
I want to handle the array according to number of properties for a category.
I have created an array(array_a) using available properties, and I group that array value according to properties(array_colors,array_sizes). for example now i have two arrays 'array_colors' and 'array_sizes', so in my code I for loop them and created an object array(array_b).
but my issue is I want to handle dynamic number of properties.
var array_a = [
{
"color":{
"id":2,
"name":"Green"
}
},
{
"color":{
"id":3,
"name":"Yellow"
}
},
{
"size":{
"id":5,
"name":"16"
}
},
{
"size":{
"id":6,
"name":"17"
}
},
{
"size":{
"id":7,
"name":"18"
}
}
];
array_colors = array_a.filter(function(myObject) {
return myObject.hasOwnProperty('color');
});
array_sizes = array_a.filter(function(myObject) {
return myObject.hasOwnProperty('size');
});
array_b = ;
for(var i = 0; i < array_colors.length; i++) {
for (var j = 0; j < array_sizes.length; j++) {
array_b.push( {
"is_available":true,
"qty":0,
"price":0,
"color":array_colors[i],
"size":array_sizes[j]
});
}
}
array_b = [
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":5,
"name":"16",
"text":"16"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":6,
"name":"17",
"text":"17"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":7,
"name":"18",
"text":"18"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":5,
"name":"16",
"text":"16"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":6,
"name":"17",
"text":"17"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":7,
"name":"18",
"text":"18"
}
}
]
my expected array as above.I want to handle the properties such as color,size dynamically.
javascript vue.js lodash
add a comment |
I want to handle the array according to number of properties for a category.
I have created an array(array_a) using available properties, and I group that array value according to properties(array_colors,array_sizes). for example now i have two arrays 'array_colors' and 'array_sizes', so in my code I for loop them and created an object array(array_b).
but my issue is I want to handle dynamic number of properties.
var array_a = [
{
"color":{
"id":2,
"name":"Green"
}
},
{
"color":{
"id":3,
"name":"Yellow"
}
},
{
"size":{
"id":5,
"name":"16"
}
},
{
"size":{
"id":6,
"name":"17"
}
},
{
"size":{
"id":7,
"name":"18"
}
}
];
array_colors = array_a.filter(function(myObject) {
return myObject.hasOwnProperty('color');
});
array_sizes = array_a.filter(function(myObject) {
return myObject.hasOwnProperty('size');
});
array_b = ;
for(var i = 0; i < array_colors.length; i++) {
for (var j = 0; j < array_sizes.length; j++) {
array_b.push( {
"is_available":true,
"qty":0,
"price":0,
"color":array_colors[i],
"size":array_sizes[j]
});
}
}
array_b = [
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":5,
"name":"16",
"text":"16"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":6,
"name":"17",
"text":"17"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":7,
"name":"18",
"text":"18"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":5,
"name":"16",
"text":"16"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":6,
"name":"17",
"text":"17"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":7,
"name":"18",
"text":"18"
}
}
]
my expected array as above.I want to handle the properties such as color,size dynamically.
javascript vue.js lodash
2
Are you sure that's your expected output?'red'
is nowhere in your input (and there don't seem to be many of the properties in the object you'repush
ing toarray_b
?)
– CertainPerformance
Dec 31 '18 at 9:56
sorry, i don't understand what you mean
– chathu
Dec 31 '18 at 10:43
You say your expected output isarray_b = [ {'color':'red','size':16}, {'color':'red','size':17}, {'color':'green','size':16}, {'color':'green','size':17} ]
, but where does'color':'red'
come from?'red'
isn't anywhere else in the script, why does it magically appear in the output? Also,green
is capitalized in your input, did you want something to calltoLowerCase
on the color when turning it intoarray_b
?
– CertainPerformance
Dec 31 '18 at 10:45
Your expected output also doesn't contain any properties other thancolor
andsize
, but your.push
attempt has an object that hasis_available
,qty
, andprice
. Are those properties actually expected in your output or not? Please clarify your question
– CertainPerformance
Dec 31 '18 at 10:47
sorry, by mistake i have put 'red'. by the way i got the answer. thank you for your kind consideration
– chathu
Dec 31 '18 at 11:02
add a comment |
I want to handle the array according to number of properties for a category.
I have created an array(array_a) using available properties, and I group that array value according to properties(array_colors,array_sizes). for example now i have two arrays 'array_colors' and 'array_sizes', so in my code I for loop them and created an object array(array_b).
but my issue is I want to handle dynamic number of properties.
var array_a = [
{
"color":{
"id":2,
"name":"Green"
}
},
{
"color":{
"id":3,
"name":"Yellow"
}
},
{
"size":{
"id":5,
"name":"16"
}
},
{
"size":{
"id":6,
"name":"17"
}
},
{
"size":{
"id":7,
"name":"18"
}
}
];
array_colors = array_a.filter(function(myObject) {
return myObject.hasOwnProperty('color');
});
array_sizes = array_a.filter(function(myObject) {
return myObject.hasOwnProperty('size');
});
array_b = ;
for(var i = 0; i < array_colors.length; i++) {
for (var j = 0; j < array_sizes.length; j++) {
array_b.push( {
"is_available":true,
"qty":0,
"price":0,
"color":array_colors[i],
"size":array_sizes[j]
});
}
}
array_b = [
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":5,
"name":"16",
"text":"16"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":6,
"name":"17",
"text":"17"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":7,
"name":"18",
"text":"18"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":5,
"name":"16",
"text":"16"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":6,
"name":"17",
"text":"17"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":7,
"name":"18",
"text":"18"
}
}
]
my expected array as above.I want to handle the properties such as color,size dynamically.
javascript vue.js lodash
I want to handle the array according to number of properties for a category.
I have created an array(array_a) using available properties, and I group that array value according to properties(array_colors,array_sizes). for example now i have two arrays 'array_colors' and 'array_sizes', so in my code I for loop them and created an object array(array_b).
but my issue is I want to handle dynamic number of properties.
var array_a = [
{
"color":{
"id":2,
"name":"Green"
}
},
{
"color":{
"id":3,
"name":"Yellow"
}
},
{
"size":{
"id":5,
"name":"16"
}
},
{
"size":{
"id":6,
"name":"17"
}
},
{
"size":{
"id":7,
"name":"18"
}
}
];
array_colors = array_a.filter(function(myObject) {
return myObject.hasOwnProperty('color');
});
array_sizes = array_a.filter(function(myObject) {
return myObject.hasOwnProperty('size');
});
array_b = ;
for(var i = 0; i < array_colors.length; i++) {
for (var j = 0; j < array_sizes.length; j++) {
array_b.push( {
"is_available":true,
"qty":0,
"price":0,
"color":array_colors[i],
"size":array_sizes[j]
});
}
}
array_b = [
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":5,
"name":"16",
"text":"16"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":6,
"name":"17",
"text":"17"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":2,
"name":"Green"
},
"size":{
"id":7,
"name":"18",
"text":"18"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":5,
"name":"16",
"text":"16"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":6,
"name":"17",
"text":"17"
}
},
{
"is_available":true,
"qty":0,
"price":0,
"color":{
"id":3,
"name":"Yellow"
},
"size":{
"id":7,
"name":"18",
"text":"18"
}
}
]
my expected array as above.I want to handle the properties such as color,size dynamically.
javascript vue.js lodash
javascript vue.js lodash
edited Jan 2 at 5:25
chathu
asked Dec 31 '18 at 9:53
chathuchathu
113
113
2
Are you sure that's your expected output?'red'
is nowhere in your input (and there don't seem to be many of the properties in the object you'repush
ing toarray_b
?)
– CertainPerformance
Dec 31 '18 at 9:56
sorry, i don't understand what you mean
– chathu
Dec 31 '18 at 10:43
You say your expected output isarray_b = [ {'color':'red','size':16}, {'color':'red','size':17}, {'color':'green','size':16}, {'color':'green','size':17} ]
, but where does'color':'red'
come from?'red'
isn't anywhere else in the script, why does it magically appear in the output? Also,green
is capitalized in your input, did you want something to calltoLowerCase
on the color when turning it intoarray_b
?
– CertainPerformance
Dec 31 '18 at 10:45
Your expected output also doesn't contain any properties other thancolor
andsize
, but your.push
attempt has an object that hasis_available
,qty
, andprice
. Are those properties actually expected in your output or not? Please clarify your question
– CertainPerformance
Dec 31 '18 at 10:47
sorry, by mistake i have put 'red'. by the way i got the answer. thank you for your kind consideration
– chathu
Dec 31 '18 at 11:02
add a comment |
2
Are you sure that's your expected output?'red'
is nowhere in your input (and there don't seem to be many of the properties in the object you'repush
ing toarray_b
?)
– CertainPerformance
Dec 31 '18 at 9:56
sorry, i don't understand what you mean
– chathu
Dec 31 '18 at 10:43
You say your expected output isarray_b = [ {'color':'red','size':16}, {'color':'red','size':17}, {'color':'green','size':16}, {'color':'green','size':17} ]
, but where does'color':'red'
come from?'red'
isn't anywhere else in the script, why does it magically appear in the output? Also,green
is capitalized in your input, did you want something to calltoLowerCase
on the color when turning it intoarray_b
?
– CertainPerformance
Dec 31 '18 at 10:45
Your expected output also doesn't contain any properties other thancolor
andsize
, but your.push
attempt has an object that hasis_available
,qty
, andprice
. Are those properties actually expected in your output or not? Please clarify your question
– CertainPerformance
Dec 31 '18 at 10:47
sorry, by mistake i have put 'red'. by the way i got the answer. thank you for your kind consideration
– chathu
Dec 31 '18 at 11:02
2
2
Are you sure that's your expected output?
'red'
is nowhere in your input (and there don't seem to be many of the properties in the object you're push
ing to array_b
?)– CertainPerformance
Dec 31 '18 at 9:56
Are you sure that's your expected output?
'red'
is nowhere in your input (and there don't seem to be many of the properties in the object you're push
ing to array_b
?)– CertainPerformance
Dec 31 '18 at 9:56
sorry, i don't understand what you mean
– chathu
Dec 31 '18 at 10:43
sorry, i don't understand what you mean
– chathu
Dec 31 '18 at 10:43
You say your expected output is
array_b = [ {'color':'red','size':16}, {'color':'red','size':17}, {'color':'green','size':16}, {'color':'green','size':17} ]
, but where does 'color':'red'
come from? 'red'
isn't anywhere else in the script, why does it magically appear in the output? Also, green
is capitalized in your input, did you want something to call toLowerCase
on the color when turning it into array_b
?– CertainPerformance
Dec 31 '18 at 10:45
You say your expected output is
array_b = [ {'color':'red','size':16}, {'color':'red','size':17}, {'color':'green','size':16}, {'color':'green','size':17} ]
, but where does 'color':'red'
come from? 'red'
isn't anywhere else in the script, why does it magically appear in the output? Also, green
is capitalized in your input, did you want something to call toLowerCase
on the color when turning it into array_b
?– CertainPerformance
Dec 31 '18 at 10:45
Your expected output also doesn't contain any properties other than
color
and size
, but your .push
attempt has an object that has is_available
, qty
, and price
. Are those properties actually expected in your output or not? Please clarify your question– CertainPerformance
Dec 31 '18 at 10:47
Your expected output also doesn't contain any properties other than
color
and size
, but your .push
attempt has an object that has is_available
, qty
, and price
. Are those properties actually expected in your output or not? Please clarify your question– CertainPerformance
Dec 31 '18 at 10:47
sorry, by mistake i have put 'red'. by the way i got the answer. thank you for your kind consideration
– chathu
Dec 31 '18 at 11:02
sorry, by mistake i have put 'red'. by the way i got the answer. thank you for your kind consideration
– chathu
Dec 31 '18 at 11:02
add a comment |
1 Answer
1
active
oldest
votes
using _.mergeWith()
you can merge the properties to a single object with the property names as the keys, and an array of values for each key.
Then reduce the object to an array of objects using _.flatMap()
(or Array.flatMap()
:
const array_a = [{"color":{"id":2,"name":"Green"}},{"color":{"id":3,"name":"Yellow"}},{"size":{"id":5,"name":"16"}},{"size":{"id":6,"name":"17"}},{"size":{"id":7,"name":"18"}}];
const getAllCombinations = _.flow([
arr => _.mergeWith({}, ...arr, (o, s) => _.isArray(o) ? [...o, s.name] : [s.name]),
props => _.reduce(props, (r, values, k) =>
_.isEmpty(r) ?
values.map(v => ({
is_available: true,
qty: 0,
price: 0,
[k]: v,
})) :
_.flatMap(r, o => values.map(v => ({
...o,
[k]: v
}))), )
]);
console.log(getAllCombinations(array_a));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
nice and quick answer. thank you for your answer
– chathu
Dec 31 '18 at 10:58
You're welcome :) Don't forget to accept.
– Ori Drori
Dec 31 '18 at 11:09
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986030%2fcreate-object-array-according-to-changing-number-of-properties%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
using _.mergeWith()
you can merge the properties to a single object with the property names as the keys, and an array of values for each key.
Then reduce the object to an array of objects using _.flatMap()
(or Array.flatMap()
:
const array_a = [{"color":{"id":2,"name":"Green"}},{"color":{"id":3,"name":"Yellow"}},{"size":{"id":5,"name":"16"}},{"size":{"id":6,"name":"17"}},{"size":{"id":7,"name":"18"}}];
const getAllCombinations = _.flow([
arr => _.mergeWith({}, ...arr, (o, s) => _.isArray(o) ? [...o, s.name] : [s.name]),
props => _.reduce(props, (r, values, k) =>
_.isEmpty(r) ?
values.map(v => ({
is_available: true,
qty: 0,
price: 0,
[k]: v,
})) :
_.flatMap(r, o => values.map(v => ({
...o,
[k]: v
}))), )
]);
console.log(getAllCombinations(array_a));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
nice and quick answer. thank you for your answer
– chathu
Dec 31 '18 at 10:58
You're welcome :) Don't forget to accept.
– Ori Drori
Dec 31 '18 at 11:09
add a comment |
using _.mergeWith()
you can merge the properties to a single object with the property names as the keys, and an array of values for each key.
Then reduce the object to an array of objects using _.flatMap()
(or Array.flatMap()
:
const array_a = [{"color":{"id":2,"name":"Green"}},{"color":{"id":3,"name":"Yellow"}},{"size":{"id":5,"name":"16"}},{"size":{"id":6,"name":"17"}},{"size":{"id":7,"name":"18"}}];
const getAllCombinations = _.flow([
arr => _.mergeWith({}, ...arr, (o, s) => _.isArray(o) ? [...o, s.name] : [s.name]),
props => _.reduce(props, (r, values, k) =>
_.isEmpty(r) ?
values.map(v => ({
is_available: true,
qty: 0,
price: 0,
[k]: v,
})) :
_.flatMap(r, o => values.map(v => ({
...o,
[k]: v
}))), )
]);
console.log(getAllCombinations(array_a));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
nice and quick answer. thank you for your answer
– chathu
Dec 31 '18 at 10:58
You're welcome :) Don't forget to accept.
– Ori Drori
Dec 31 '18 at 11:09
add a comment |
using _.mergeWith()
you can merge the properties to a single object with the property names as the keys, and an array of values for each key.
Then reduce the object to an array of objects using _.flatMap()
(or Array.flatMap()
:
const array_a = [{"color":{"id":2,"name":"Green"}},{"color":{"id":3,"name":"Yellow"}},{"size":{"id":5,"name":"16"}},{"size":{"id":6,"name":"17"}},{"size":{"id":7,"name":"18"}}];
const getAllCombinations = _.flow([
arr => _.mergeWith({}, ...arr, (o, s) => _.isArray(o) ? [...o, s.name] : [s.name]),
props => _.reduce(props, (r, values, k) =>
_.isEmpty(r) ?
values.map(v => ({
is_available: true,
qty: 0,
price: 0,
[k]: v,
})) :
_.flatMap(r, o => values.map(v => ({
...o,
[k]: v
}))), )
]);
console.log(getAllCombinations(array_a));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
using _.mergeWith()
you can merge the properties to a single object with the property names as the keys, and an array of values for each key.
Then reduce the object to an array of objects using _.flatMap()
(or Array.flatMap()
:
const array_a = [{"color":{"id":2,"name":"Green"}},{"color":{"id":3,"name":"Yellow"}},{"size":{"id":5,"name":"16"}},{"size":{"id":6,"name":"17"}},{"size":{"id":7,"name":"18"}}];
const getAllCombinations = _.flow([
arr => _.mergeWith({}, ...arr, (o, s) => _.isArray(o) ? [...o, s.name] : [s.name]),
props => _.reduce(props, (r, values, k) =>
_.isEmpty(r) ?
values.map(v => ({
is_available: true,
qty: 0,
price: 0,
[k]: v,
})) :
_.flatMap(r, o => values.map(v => ({
...o,
[k]: v
}))), )
]);
console.log(getAllCombinations(array_a));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
const array_a = [{"color":{"id":2,"name":"Green"}},{"color":{"id":3,"name":"Yellow"}},{"size":{"id":5,"name":"16"}},{"size":{"id":6,"name":"17"}},{"size":{"id":7,"name":"18"}}];
const getAllCombinations = _.flow([
arr => _.mergeWith({}, ...arr, (o, s) => _.isArray(o) ? [...o, s.name] : [s.name]),
props => _.reduce(props, (r, values, k) =>
_.isEmpty(r) ?
values.map(v => ({
is_available: true,
qty: 0,
price: 0,
[k]: v,
})) :
_.flatMap(r, o => values.map(v => ({
...o,
[k]: v
}))), )
]);
console.log(getAllCombinations(array_a));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
const array_a = [{"color":{"id":2,"name":"Green"}},{"color":{"id":3,"name":"Yellow"}},{"size":{"id":5,"name":"16"}},{"size":{"id":6,"name":"17"}},{"size":{"id":7,"name":"18"}}];
const getAllCombinations = _.flow([
arr => _.mergeWith({}, ...arr, (o, s) => _.isArray(o) ? [...o, s.name] : [s.name]),
props => _.reduce(props, (r, values, k) =>
_.isEmpty(r) ?
values.map(v => ({
is_available: true,
qty: 0,
price: 0,
[k]: v,
})) :
_.flatMap(r, o => values.map(v => ({
...o,
[k]: v
}))), )
]);
console.log(getAllCombinations(array_a));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
edited Dec 31 '18 at 11:20
answered Dec 31 '18 at 10:25
Ori DroriOri Drori
76.7k138192
76.7k138192
nice and quick answer. thank you for your answer
– chathu
Dec 31 '18 at 10:58
You're welcome :) Don't forget to accept.
– Ori Drori
Dec 31 '18 at 11:09
add a comment |
nice and quick answer. thank you for your answer
– chathu
Dec 31 '18 at 10:58
You're welcome :) Don't forget to accept.
– Ori Drori
Dec 31 '18 at 11:09
nice and quick answer. thank you for your answer
– chathu
Dec 31 '18 at 10:58
nice and quick answer. thank you for your answer
– chathu
Dec 31 '18 at 10:58
You're welcome :) Don't forget to accept.
– Ori Drori
Dec 31 '18 at 11:09
You're welcome :) Don't forget to accept.
– Ori Drori
Dec 31 '18 at 11:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986030%2fcreate-object-array-according-to-changing-number-of-properties%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Are you sure that's your expected output?
'red'
is nowhere in your input (and there don't seem to be many of the properties in the object you'repush
ing toarray_b
?)– CertainPerformance
Dec 31 '18 at 9:56
sorry, i don't understand what you mean
– chathu
Dec 31 '18 at 10:43
You say your expected output is
array_b = [ {'color':'red','size':16}, {'color':'red','size':17}, {'color':'green','size':16}, {'color':'green','size':17} ]
, but where does'color':'red'
come from?'red'
isn't anywhere else in the script, why does it magically appear in the output? Also,green
is capitalized in your input, did you want something to calltoLowerCase
on the color when turning it intoarray_b
?– CertainPerformance
Dec 31 '18 at 10:45
Your expected output also doesn't contain any properties other than
color
andsize
, but your.push
attempt has an object that hasis_available
,qty
, andprice
. Are those properties actually expected in your output or not? Please clarify your question– CertainPerformance
Dec 31 '18 at 10:47
sorry, by mistake i have put 'red'. by the way i got the answer. thank you for your kind consideration
– chathu
Dec 31 '18 at 11:02