技術情報

Dart Object ListのKey-valueで重複を削除する方法

DartでKey-ValueによるObjectのリストから重複を削除するには、toSet()が利用できます。最初にtoSetメソッドを呼び出し、その後、再びリストに変換するだけです。

以下に例を示します。

class _Product {
  final name;
  final type;
  final price;
  _Product({
    required this.name,
    required this.type,
    required this.price,
  });

  String toJson() {
    return "{ name: $name, type: $type, price: $price}";
  }
}

toSetを呼び出してみます。

final products = [
  _Product(name: "USB", type: "A", price: 10), // same
  _Product(name: "USB", type: "A", price: 10), // same
  _Product(name: "USB", type: "B", price: 12),
  _Product(name: "USB", type: "C", price: 11),
  _Product(name: "Mouse", type: "A", price: 10),
  _Product(name: "Mouse", type: "B", price: 12), // same
  _Product(name: "Mouse", type: "B", price: 12), // same
  _Product(name: "Mouse", type: "C", price: 10),
  _Product(name: "Laptop", type: "A", price: 100),
  _Product(name: "Laptop", type: "B", price: 120), // same
  _Product(name: "Laptop", type: "B", price: 120), // same
];

print(products.length); // 11
print(products.toSet().length); // 11

toSet()で呼び出しても、同じ長さになっています。その時、hashCodeを確認してみます。

products.forEach((element) {
    print(element.hashCode);
    // 257416280
    // 1045932166
    // 730517725
    // 586146378
    // 215518337
    // 471982393
    // 775971279
    // 335683510
    // 844714385
    // 633234047
    // 1021163746
});

それぞれのhashCodeは異なっているため、hashCodeと==演算子をオーバーライドする必要があります。

class _Product {
  ...

  @override
  bool operator ==(Object other) {
    return other is _Product && other.hashCode == hashCode;
  }

  @override
  int get hashCode => Object.hash(name, type, price);
}

このクラスはリストを持ちませんから、Object.hash関数を使用しますが、リスト変数を含む場合は、代わりにObject.hashAllを使用します。

もう一度、toSetを呼び出してみます。

print(products.length); // 11
print(products.toSet().length); // 8
products.toSet().forEach((element) => print(element.toJson()));
// { name: USB, type: A, price: 10}
// { name: USB, type: B, price: 12}
// { name: USB, type: C, price: 11}
// { name: Mouse, type: A, price: 10}
// { name: Mouse, type: B, price: 12}
// { name: Mouse, type: C, price: 10}
// { name: Laptop, type: A, price: 100}
// { name: Laptop, type: B, price: 120}

なお、重複するものはここでは表示しませんでした。

金曜担当 – Ami



正規表現基礎(4)

今回はさまざまなプログラミング言語やツールで活用できる正規表現の記号を紹介します。
本記事は前回の「正規表現基礎(3)」の続きです。

続きを読む

Checking if a file already exists in the given path with Node js

Today, I would like to share about checking if a file already exists in the given path using Node js. Let’s take a look.

To check if a file already exists or not, we can perform simply by using existsSync() method of ‘fs’ module in node js. The code is as follow.

const fs = require('fs')

let filePath = "C:\\Users\\waith\\Documents\\t1ee.pdf"
if (!fs.existsSync(filePath)) {
  console.log('File does NOT exist!');
}else{
  console.log('File exists.');
}

Yes, that is it. The steps to perfom that is very simple. It is just to declare fs module and use fs.existsSync() method to check the filepath.

This is all for now. Hope you enjoy that.

By Asahi



3 Useful image manipulation libraries in Node JS

Today, I would like to share about image manipulation libraries in Node JS. Let’s take a look.

1. Canvas

Node Canvas is a very powerful image library which implements Web Canvas API. So it will be flexible for developers.

Check here for more details.

2. Sharp

Sharp is an efficient image processing library for Node JS. It is very useful for resizing, converting and editing images.

Check here for more details.

3. GraphicsMagick

GraphicsMagick is also a powerful image processing library of Node JS for many image functions.

Check here for more details.

This is all for now. Hope you enjoy that.

By Asahi



Instagram is releasing its age verification system in more areas

Meta announced that it has begun testing Instagram’s age verification tool in more countries, including Canada and Mexico. Last June, this social he networking app began testing new options for users to verify their age. Use one of the three options. Upload your ID, record a video selfie, or ask a mutual friend to verify your age. When a user tries to edit her date of birth on her Instagram from her 18 to her 18+, the app asks her to verify her age using one of three methods.

It started at the United States and expanded to Brazil and Japan in last October. The age verification tool is currently being tested in more countries in Europe, Mexico, Canada, South Korea, Australia, and Japan. Meta plans to make the tool available globally in the coming months.

Product mocks of age verification
Image Credit: Meta

To verify your age, you can show your ID, such as your passport or driver’s license. In that case, Instagram will store her ID on its servers for 30 days before deleting it. If you do not have valid ID, you can choose the selfie video method. Instagram has partnered with London-based digital identity startup Yoti to offer this option. When you upload a selfie video, it will be shared with Yoti who uses specially trained AI to check your age. Both companies delete the data once the verification process is complete.

Product mocks of ID upload screens
Image Credit : Meta

A third age verification option, called “social verification,” allows mutual followers to verify their age. The nominator must be 18 years of age or older and may not nominate on behalf of someone else at that time. Her three chosen guarantors must receive age verification requests and respond within three days. Those who vouch for you will get the option to specify an age range and All three must select the same option to pass age verification.

Product mocks for age verification
Image Credit : Meta

Yuuma




アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム