Filter repeated keys
Please I'm new to this. I have searched, what I saw left me confused.
How can I reduce duplicated items in an array and add the values of the duplicated key to the unique one like below.
I want to have it like arrayA
let arrayA = [
name: "temperature", data: "2000-09-26": 82 ,
name: "temperature", data: "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79 ,
name: "frequency", data: "2004-01-17": 16 ,
name: "current", data: "1997-02-14": 65
]
let arrayB = [
name: "temperature", data: "2000-09-26": 82, "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79, "1997-02-14": 65 ,
name: "frequency", data: "2004-01-17": 16
]
javascript arrays
add a comment |
Please I'm new to this. I have searched, what I saw left me confused.
How can I reduce duplicated items in an array and add the values of the duplicated key to the unique one like below.
I want to have it like arrayA
let arrayA = [
name: "temperature", data: "2000-09-26": 82 ,
name: "temperature", data: "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79 ,
name: "frequency", data: "2004-01-17": 16 ,
name: "current", data: "1997-02-14": 65
]
let arrayB = [
name: "temperature", data: "2000-09-26": 82, "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79, "1997-02-14": 65 ,
name: "frequency", data: "2004-01-17": 16
]
javascript arrays
1
would you like to go from a to b or from b to a? btw, what have you tried?
– Nina Scholz
Nov 16 '18 at 8:44
I want to go from a to b
– obi patrick
Nov 16 '18 at 8:47
What I tried was using variable as the key
– obi patrick
Nov 16 '18 at 8:49
add a comment |
Please I'm new to this. I have searched, what I saw left me confused.
How can I reduce duplicated items in an array and add the values of the duplicated key to the unique one like below.
I want to have it like arrayA
let arrayA = [
name: "temperature", data: "2000-09-26": 82 ,
name: "temperature", data: "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79 ,
name: "frequency", data: "2004-01-17": 16 ,
name: "current", data: "1997-02-14": 65
]
let arrayB = [
name: "temperature", data: "2000-09-26": 82, "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79, "1997-02-14": 65 ,
name: "frequency", data: "2004-01-17": 16
]
javascript arrays
Please I'm new to this. I have searched, what I saw left me confused.
How can I reduce duplicated items in an array and add the values of the duplicated key to the unique one like below.
I want to have it like arrayA
let arrayA = [
name: "temperature", data: "2000-09-26": 82 ,
name: "temperature", data: "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79 ,
name: "frequency", data: "2004-01-17": 16 ,
name: "current", data: "1997-02-14": 65
]
let arrayB = [
name: "temperature", data: "2000-09-26": 82, "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79, "1997-02-14": 65 ,
name: "frequency", data: "2004-01-17": 16
]
javascript arrays
javascript arrays
asked Nov 16 '18 at 8:42
obi patrickobi patrick
214
214
1
would you like to go from a to b or from b to a? btw, what have you tried?
– Nina Scholz
Nov 16 '18 at 8:44
I want to go from a to b
– obi patrick
Nov 16 '18 at 8:47
What I tried was using variable as the key
– obi patrick
Nov 16 '18 at 8:49
add a comment |
1
would you like to go from a to b or from b to a? btw, what have you tried?
– Nina Scholz
Nov 16 '18 at 8:44
I want to go from a to b
– obi patrick
Nov 16 '18 at 8:47
What I tried was using variable as the key
– obi patrick
Nov 16 '18 at 8:49
1
1
would you like to go from a to b or from b to a? btw, what have you tried?
– Nina Scholz
Nov 16 '18 at 8:44
would you like to go from a to b or from b to a? btw, what have you tried?
– Nina Scholz
Nov 16 '18 at 8:44
I want to go from a to b
– obi patrick
Nov 16 '18 at 8:47
I want to go from a to b
– obi patrick
Nov 16 '18 at 8:47
What I tried was using variable as the key
– obi patrick
Nov 16 '18 at 8:49
What I tried was using variable as the key
– obi patrick
Nov 16 '18 at 8:49
add a comment |
3 Answers
3
active
oldest
votes
You can use array#reduce
and Object.values()
. Create a hash of name
and add the data
value for a given name
. Then get the values from the object using Object.values()
.
let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
65 ],
result = Object.values(arrayA.reduce((r,name, data) => ,));
console.log(result);
add a comment |
Here you are
let arrayB = arrayA.reduce((acc, curr) =>
const found = acc.find(i => i.name === curr.name)
if (found)
// If this object is already there, merge their `data` property
Object.assign(found.data, curr.data)
else
// Otherwise add to the accumulator (`acc`)
acc.push(curr)
return acc
, )
Reference to the reduce function:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
add a comment |
var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)
return _.reduce(list,function(nObj,obj)
if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
nObj.data=_.assign(nObj.data,obj.data)
else
nObj=obj
return nObj
,)
)
console.log(arrayB)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>
add a comment |
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%2f53334211%2ffilter-repeated-keys%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use array#reduce
and Object.values()
. Create a hash of name
and add the data
value for a given name
. Then get the values from the object using Object.values()
.
let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
65 ],
result = Object.values(arrayA.reduce((r,name, data) => ,));
console.log(result);
add a comment |
You can use array#reduce
and Object.values()
. Create a hash of name
and add the data
value for a given name
. Then get the values from the object using Object.values()
.
let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
65 ],
result = Object.values(arrayA.reduce((r,name, data) => ,));
console.log(result);
add a comment |
You can use array#reduce
and Object.values()
. Create a hash of name
and add the data
value for a given name
. Then get the values from the object using Object.values()
.
let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
65 ],
result = Object.values(arrayA.reduce((r,name, data) => ,));
console.log(result);
You can use array#reduce
and Object.values()
. Create a hash of name
and add the data
value for a given name
. Then get the values from the object using Object.values()
.
let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
65 ],
result = Object.values(arrayA.reduce((r,name, data) => ,));
console.log(result);
let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
65 ],
result = Object.values(arrayA.reduce((r,name, data) => ,));
console.log(result);
let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
65 ],
result = Object.values(arrayA.reduce((r,name, data) => ,));
console.log(result);
answered Nov 16 '18 at 8:49
Hassan ImamHassan Imam
12k31532
12k31532
add a comment |
add a comment |
Here you are
let arrayB = arrayA.reduce((acc, curr) =>
const found = acc.find(i => i.name === curr.name)
if (found)
// If this object is already there, merge their `data` property
Object.assign(found.data, curr.data)
else
// Otherwise add to the accumulator (`acc`)
acc.push(curr)
return acc
, )
Reference to the reduce function:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
add a comment |
Here you are
let arrayB = arrayA.reduce((acc, curr) =>
const found = acc.find(i => i.name === curr.name)
if (found)
// If this object is already there, merge their `data` property
Object.assign(found.data, curr.data)
else
// Otherwise add to the accumulator (`acc`)
acc.push(curr)
return acc
, )
Reference to the reduce function:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
add a comment |
Here you are
let arrayB = arrayA.reduce((acc, curr) =>
const found = acc.find(i => i.name === curr.name)
if (found)
// If this object is already there, merge their `data` property
Object.assign(found.data, curr.data)
else
// Otherwise add to the accumulator (`acc`)
acc.push(curr)
return acc
, )
Reference to the reduce function:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
Here you are
let arrayB = arrayA.reduce((acc, curr) =>
const found = acc.find(i => i.name === curr.name)
if (found)
// If this object is already there, merge their `data` property
Object.assign(found.data, curr.data)
else
// Otherwise add to the accumulator (`acc`)
acc.push(curr)
return acc
, )
Reference to the reduce function:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
answered Nov 16 '18 at 8:53
Nurbol AlpysbayevNurbol Alpysbayev
4,8091533
4,8091533
add a comment |
add a comment |
var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)
return _.reduce(list,function(nObj,obj)
if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
nObj.data=_.assign(nObj.data,obj.data)
else
nObj=obj
return nObj
,)
)
console.log(arrayB)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>
add a comment |
var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)
return _.reduce(list,function(nObj,obj)
if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
nObj.data=_.assign(nObj.data,obj.data)
else
nObj=obj
return nObj
,)
)
console.log(arrayB)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>
add a comment |
var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)
return _.reduce(list,function(nObj,obj)
if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
nObj.data=_.assign(nObj.data,obj.data)
else
nObj=obj
return nObj
,)
)
console.log(arrayB)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>
var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)
return _.reduce(list,function(nObj,obj)
if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
nObj.data=_.assign(nObj.data,obj.data)
else
nObj=obj
return nObj
,)
)
console.log(arrayB)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>
var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)
return _.reduce(list,function(nObj,obj)
if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
nObj.data=_.assign(nObj.data,obj.data)
else
nObj=obj
return nObj
,)
)
console.log(arrayB)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>
var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)
return _.reduce(list,function(nObj,obj)
if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
nObj.data=_.assign(nObj.data,obj.data)
else
nObj=obj
return nObj
,)
)
console.log(arrayB)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>
answered Nov 16 '18 at 9:35
Pritam JanaPritam Jana
10913
10913
add a comment |
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%2f53334211%2ffilter-repeated-keys%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
1
would you like to go from a to b or from b to a? btw, what have you tried?
– Nina Scholz
Nov 16 '18 at 8:44
I want to go from a to b
– obi patrick
Nov 16 '18 at 8:47
What I tried was using variable as the key
– obi patrick
Nov 16 '18 at 8:49