使用jQuery,我如何迭代一个对象,并获得具有每个值的计数的键的唯一值?
例如,对于此数组:
var electrons = [
{ name: 'Electron1', distance: 1 },
{ name: 'Electron2', distance: 1 },
{ name: 'Electron3', distance: 2 },
{ name: 'Electron4', distance: 2 },
{ name: 'Electron5', distance: 2 },
{ name: 'Electron6', distance: 2 },
{ name: 'Electron7', distance: 2 },
{ name: 'Electron8', distance: 2 },
{ name: 'Electron9', distance: 2 },
{ name: 'Electron10', distance: 2 },
{ name: 'Electron11', distance: 3 },
];
Run Code Online (Sandbox Code Playgroud)
我想取回以下内容:
var distance_counts = {1: 2, 2: …Run Code Online (Sandbox Code Playgroud) 我有一个重复字母的字符串.我希望不止一次重复的字母只显示一次.例如,我有一个字符串aaabbbccc我希望结果是abc.到目前为止我的功能是这样的:
function unique_char(string) {
var unique = '';
var count = 0;
for (var i = 0; i < string.length; i++) {
for (var j = i+1; j < string.length; j++) {
if (string[i] == string[j]) {
count++;
unique += string[i];
}
}
}
return unique;
}
document.write(unique_char('aaabbbccc'));
Run Code Online (Sandbox Code Playgroud)
该函数必须在循环内循环; 这就是为什么第二个for在第一个内部.
我想用DOM元素作为键创建一个哈希.这由以下代码说明:
var hash = {};
var set = function(element, value) { hash[element] = value; };
var get = function(element) { return hash[element]; };
set(document.getElementById('foo'), 'bar');
get(document.getElementById('foo')); // returns 'bar'
Run Code Online (Sandbox Code Playgroud)
如何确保哈希映射到每个哈希值的唯一值Element?
请注意,我不能将原始ID字符串用作键,因为Element可以传入任意字符串,包括那些没有id的字符串.
我有一个带有obejcts电子邮件和Id的数组,所以我想要删除具有相似ID的重复元素.
例:
var newarray=[
{
Email:"test1@gmail.com",
ID:"A"
},
{
Email:"test2@gmail.com",
ID:"B"
},
{
Email:"test3@gmail.com",
ID:"A"
},
{
Email:"test4@gmail.com",
ID:"C"
},
{
Email:"test4@gmail.com",
ID:"C"
}
];
Run Code Online (Sandbox Code Playgroud)
现在我需要删除ID很常见的重复元素.我期待最终的数组是
var FinalArray=[
{
Email:"test1@gmail.com",
ID:"A"
},
{
Email:"test2@gmail.com",
ID:"B"
},
{
Email:"test5@gmail.com",
ID:"C"
}
];
Run Code Online (Sandbox Code Playgroud) 如何使用Array.filter()返回唯一id的name?
我的场景与我研究的解决方案略有不同,我有一个对象数组.我找到的每个例子都包含一个单值的平面数组.
data=[
{id: 555, name: "Sales", person: "Jordan" },
{id: 555, name: "Sales", person: "Bob" },
{id: 555, name: "Sales", person: "John" },
{id: 777, name: "Accounts Payable", person: "Rhoda" },
{id: 777, name: "Accounts Payable", person: "Harry" },
{id: 888, name: "IT", person: "Joe" },
{id: 888, name: "IT", person: "Jake" },
];Run Code Online (Sandbox Code Playgroud)
var unique = data.filter(
function (x, i) {
return data[i].id.indexOf(x.id) === i
});Run Code Online (Sandbox Code Playgroud)
提前致谢.
我知道如何分别做这两个事情,但是我确信必须有一种将它们组合在一起的方法。
我有一组类别,这些类别是从一组对象中提取的:
this.videoCategories = this.videos.map(v => v.category);
Run Code Online (Sandbox Code Playgroud)
但是,当然,此数组中有重复项。所以现在我做了
this.uniqueVideoCategories = this.videoCategories.filter((item, index) => {
return this.videoCategories.indexOf(item) === index;
});
Run Code Online (Sandbox Code Playgroud)
效果很好,我得到了一系列没有重复的类别。但是我试图通过将它们串在一起来学习和弄清代码,但这不起作用-产生空数组
constructor(private videoService: VideoService) {
this.videos = videoService.getVideos();
this.videoCategories = this.videos
.map(v => v.category)
.filter((item, index) => {
return this.videoCategories.indexOf(item) === index;
});
console.log(this.videoCategories);
}
Run Code Online (Sandbox Code Playgroud) 我想在数组中添加 8 个单击按钮的值。
当用户按下任何按钮时,该按钮的 id/值存储在数组中,但我的数组仅将数组中的第一个值保存在数组 [0] 处。
如何在数组中插入其他值?
这是代码:
function reply_click(obj) {
var id = [obj.id];
for (i = 0; i <= id; i++) {
alert(id);
}
}
var arr = [id];
alert(arr);
if (id[0] == "1") {
alert(6666666666);
}
if (id[0] == "2") {
id[0] = 1;
id[1] = 2;
alert(777777777777);
}
if (id[0] == "3") {
id[0] = 1;
id[1] = 2;
id[2] = 3;
alert('login');
}
alert(id);
$(document).on('click', '.btn', function() {
alert($(this).val());
var myvalues = $(this).val();
alert(myvalues); …Run Code Online (Sandbox Code Playgroud)我想在一个新数组中推送3个数组的值,而不重复相同的值
var a = ["1", "2", "3"];
var b = ["3", "4", "5"];
var c = ["4", "5", "6"];
var d = [];
function newArray(x, y, z) {
for(var i = 0; i < d.length; i++) {
if(d.length == -1) {
d[i].push(a[i])
}
}
for(var i = 0; i < d.length; i++) {
if(d.length == -1) {
d[i].push(y[i])
}
}
for(var i = 0; i < d.length; i++) {
if(d.length == -1) {
d[i].push(z[i])
}
}
}
newArray(a, b, c); …Run Code Online (Sandbox Code Playgroud) 我有以下数组
let arr = [
[
"s1@example.com",
"s2@example.com"
],
[
"s1@example.com",
"s3@example.com"
]
]
Run Code Online (Sandbox Code Playgroud)
我想从这个数组中获取唯一值。所以我期待我的结果是这样的
[
[
"s1@example.com",
"s2@example.com",
"s3@example.com"
]
]
Run Code Online (Sandbox Code Playgroud)
我使用了数组唯一函数但无法获得结果
var new_array = arr[0].concat(arr[1]);
var uniques = new_array.unique();
Run Code Online (Sandbox Code Playgroud)
如果我有两个索引,这会起作用,但是对于多个索引呢?
我有一个像这样的数组:
let x = [[1, 2], [3, 4], [1, 2], [2, 1]];
Run Code Online (Sandbox Code Playgroud)
我该怎么办才能检索没有重复项的数组?
[[1, 2], [3, 4], [2, 1]];
Run Code Online (Sandbox Code Playgroud)
我想使用过滤器方法。我试过了,但是没有用:
x.filter((value,index,self) => (self.indexOf(value) === index))
Run Code Online (Sandbox Code Playgroud)
编辑:正如我指定要使用过滤器方法,我不认为这个问题是重复的。另外,我得到了几个有趣的答案。